Written by .NET Online Instructor , Saturday, April 9, 2016 at 8:24 PM
Learning C# for beginners can be little difficult in the first place. But it isn’t that difficult.
When starting to learn C# you should start with the basics and not get into the deep rabbit hole of it.
This is Day 2 of Learn C# in 7 Days. Checkout Day 1 before starting with Day 2.
A class is a template for creating an object.
public class Employee
{
// This is a constructor public Employee()
{
}
// This is the Data public string Name;
public int Age;
// This is a method which performs actions on the data
public void DisplayData()
{
Console.WriteLine(Name + Age);
}
}
An Object is an instance of a class. In simple words, a snapshot or copy of a class having all the data and methods associated with it.
public class Program
{
static void Main()
{
Employee abhishekluv = new Employee();
abhishekluv.DisplayData();
}
}
The new
keyword + Employee()
the constructor new Employee()
is used to create an object of a class i.e. an instance of a class.
Every time you declare a class in C# there is a method Public Employee()
created in the background automatically by the compiler having the same name as the class.
void
Play with the constructor
public Employee()
{
Console.WriteLine("Constructor is fired");
}
Constructor with Parameters and Arguments
For example you have declared a class with two public variables and one public method.
As constructors are used to initial default values during object creation you can define those values using a parameterized constructor.
class Employee
{
public string Name;
public int Age;
public Employee()
{
Name = 6;
Age = 5;
Console.WriteLine("Ctor fired");
}
public Employee(string name, int age)
{
this.Name = name;
this.Age = age;
}
public void DisplayData()
{
Console.WriteLine(Name + Age);
}
}
// creating a new Employee object by calling the parameterized constructor
Employee abhishekluv = new Employee(3,4);
When an object of a class is created using the new
keyword the constructor method is called to initialize values or get and set private variables using public method parameters and arguments.
In the above code example public Employee(string name, int age)
is a parameterized constructor which has two parameters string name
and int age
.
this
keyword is used to access the class level data members, variables, and methods.
For example: In the above code, the public Employee(string name, int age)
method takes in two arguments or data values for the two defined parameters string name
and int age
. At the time of object creation these values are applied to the member variables within the class using the this keyword this.Name
and this.Age
.
Why use this
keyword? Because it makes it easy to differentiate between the declared method parameters and the class member variables with the same name. name
is the parameter declared for the DisplayData
methods and this.Name
is used to access the class level member variables.
Ever wondered why the hell you keep seeing the void
keyword everywhere when programming in C#.
Let’s talk about it.
Every method performs actions using the data variables and members defined within a class. After performing those actions the method has to return the results of those actions or sometimes not return anything.
void
keyword is used when you don’t want a method to return anything to the calling method/function. And when you want to return something using a method then you will have to specify the return data type of that method.
Example:
Note: Method names used in these examples are for educational purpose only. :P
Public void IDontReturnAnything(){
Console.WriteLine("Hello"); }
Public string IReturnAStringValue()
return "Hello";
}
Public string IReturnAIntValue()
return 143;
}
One or more variables declared with the Method Signature are called Parameters.
For example:
public double FindArea(int length, int width) { }
In the above code example int length
and int width
are parameters declared for the FindArea()
When a method is declared with parameters then it means that method can accept input values or in other words, it can accept arguments.
The values which we pass to method parameters are called Arguments
.
While calling the FindArea()
method we can pass the values as argument which the FindArea()
method can use for processing like this FindArea(3,4)
.
this
keyword in C# is used to access the class level members and data.
In a C# class, there are different types of variables.
For example:
You have declared a class level variable as int age;
and method level parameter variable as int age
.
To access class level age use this.age
and for method level parameter variable use age
.
public class Employee
{
public int age;
// this is a parameterized constructor with age as parameter and initializing the class level age variable // using the this keyword
public Employee(int age)
{
// Here we are using this.age to access the class level variables and then assigning the input value of age
// coming from the age parameter of the constructor
this.age = age;
}
public DisplayEmployeeAage()
{
Console.WriteLine("Age is = " + age);
}
}
Any programming language which follows 3 rules of OOP is called an Object-Oriented Programming Language.
Rules for OOP:
Let’s discuss each of these types in detail.
Encapsulation
Encapsulation is the concept of writing programs with data and methods that act on the data.
With encapsulation also comes another concept called Data Hiding. In data hiding we keep the data safe within the class and only accessible within the class. Now, that doesn’t mean we won’t be able to access the class data outside it. Yes we can access the data using public methods declared within a class.
Data hiding concept can be applied by using access modifiers in front of classes, member variables, and methods.
Commonly used access modifiers:
Example:
public class Sample
{
private int age;
public int salary { get; private set; }
public int DisplaySalary()
{
return this.salary;
}
public void SetData(int age, int salary)
{
this.age = age;
this.salary = salary;
}
public void DisplayData()
{
Console.Write("age is = " + age);
Console.Write("salary is = " + salary);
}
}
public class TestSample
{
static void Main()
{
Sample sample = new Sample();
sample.SetData(2, 333);
sample.DisplayData();
//the age variable of the sample cannot be accessed here
// is inaccessible due to protection level
// sample.age;
// this is encapsulation and data hiding
Console.Write("\n");
Console.Write("salary is = " + sample.DisplaySalary());
Console.ReadLine();
}
}
In order to implement data hiding we can use getter
and setter
to limited read and write access to a particular data member in a class.
In this example we have used get
and private set
for the salary property public int salary { get; private set; }
.
get
means salary
property can be accessed outside of the Sample
class and private set
means salary
property data can be set within the Sample
class only and not from outside of the class.
Polymorphism
Polymorphism allows us to define multiple functions/methods/subroutines with the same name which are differentiated by their passing parameters and arguments. It is also known as method overloading or function overloading.
Example:
Let define a Student
class with three methods with the same name.
public class Student{
public string name;
public int age;
public Student(){
// this is the constructor
}
public void DisplayData(string name){
Console.WriteLine("Student Name is :" + name);
}
public void DisplayData(int age){
Console.WriteLine("Student Age is: " + age);
}
}
Here, we are creating an instance of the Student
class and calling the DisplayData
method using different argument values. In this case, we are passing a “Abhishek” as an argument for the name
parameter and 27 as an argument for the age
parameter. To make it simple to understand when we pass a name then the first DisplayData
method will be called and for age the second DisplayData
method will be called. This is Method Overloading.
class TestStudent
{
static void Main()
{
Student newStudent = new Student();
newStudent.DisplayData("Abhishek");
newStudent.DisplayData(27);
}
}
Day 2 over
Stay Tuned.
Take Care!
Abhishek Luv
by Abhishek Luv, Sunday, February 9, 2020 at 9:36 PM
Read moreby Abhishek Luv, Sunday, January 26, 2020 at 11:33 PM
Read moreby Abhishek Luv, Saturday, January 5, 2019 at 6:30 PM
Read more