Class And Object

Class is a building block of software application. Every class responsible for particular behavior of an application.

The real application consists of at least 3 layers.

  1. Presentation Layer – UI
  2. Business Layer- Domain Logic
  3. Data Access Layer

In a class generally,have

  1. Data- it is represented by fields
  2. Behavior- It is represented by methods orfunctions
Class And Object

Constructor: Constructor has the same name as class name and it’s having no return type. If the name is not the same as a class then compiler treats this as a method. We need Constructor if we want to do some initialization. A constructor is a method that is called when an instance of a class is created. Write a constructor only when an object needs to be initialized. Constructor does not have a return type and name should the same as the class name. You can overload a constructor only signature need to be different.

this Keyword:  Is a keyword which references the current object.

When you use the different parameterized constructor and in case you need to call different default or single constructor from another constructor then you can use: this() after the whatever constructor call you to want to access. This shows an in below image.

Class And Object

But in general, this is not a good practice because it makes control flow a little bit complicated. When really need this then and then only you can use this. We can pass the control from one constructor to another by using this.

Object:It is an instance of the class and resides in memory. If student as a class we have objects like sagar, ram.

Class is a blueprint from which we create the object. In general,C# uses Pascal Case for naming convention. In this First word is always inUpperCase and Second is Camel Case in this Camel case after the first word is in Capital Letter.

In C# two types of class members present

  1. Instance: It is accessible from an object

Var stud=new Student();
Stud._name=”sagar”;

  • Static: It is accessible from the class name

Student.SaticMethod();

Object Initializer:

We are not writingconstructor in class and we can able to initializeproperties of class by using object initializer.

class Home
{
public bool isLight { get; set; }
public int noOfRooms { get; set; }
public int noOfWindows { get; set; }
public string color { get; set; }
}

var home = new Home
{
color=”white”,
isLight=true,
noOfRooms=4,
noOfWindows=8
};

Class And Object

Methods In Class:

Mainly signature of method consists of Name of that method and Number and Type of parameters.

public static void SetBoth(string Name,int Id)
{
Console.WriteLine(“Employe Name {0} and Id Is {1}”,Name,Id);
}

Overloading of methods means method have same name but different signatures.

public static void SetBoth(string Name)
{
Console.WriteLine(“Employe Name {0} “, Name);
}
public static void SetBoth(string Name,int Id)
{
Console.WriteLine(“Employe Name {0} and Id Is {1}”,Name,Id);
}

Class And Object

Params Modifier in Method – It is used only when your methods needs varying number of parameters.

public void AddNumbers(params int [] numbers)
{
int result = 0;
foreach (int i in numbers)
result += i;
Console.WriteLine(result);
}

AccessModifiers: 5 access modifiers. Itis used to control the access outside ofour class. It is for safety for our application.

  1. Public :
  2. Private
  3. Protected
  4. Internal
  5. Protected internal

Encapsulation:– In real time you define fields as private and Providegetter/setter methods as public. Means in this scenario our fields asimplementation details are hidden fromoutside and end user not know how class store data.

In this you declared the field as a private field and provide getter and setter methods for this by this you are able to hide implementation.

class Home
{
private string HomeColor;

public void SetName(string Color)
{
this.HomeColor = Color;
}
public string GetName()
{
return this.HomeColor;
}
}
class Program
{
static void Main(string[] args)
{
Home home = new Home();
home.SetName(“red”);
var color=home.GetName();

}
}

Class And Object

As per oops object should hide their implementation details and reveals what they do. In a real-world application, we are not doing the same as the create 2 functions for one field this can achieve through properties also.

Property: it is a class member that encapsulates a getter and setter method for accessing field. It is used for creating getter and setter with less code. You can write code or logic in properties.

class Home
{
private string _HomeColor;

public string homeColor {
get {
return _HomeColor;
}
set {
this._HomeColor = value;
}
}
}
class Program
{
static void Main(string[] args)
{
Home home = new Home();
home.homeColor = “red”;

}
}

Auto-Implemented Property-
class Home
{
public string Hcolor { get; set; }
}

this is auto implementedproperty which doesn’t have a private field. In the above case, we are notcreating a private field for thatproperty so compiler automatically creates the fieldas _backfield you can see this by using the ildasmtool.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *