Object-oriented programming
Classes
A class is a construct that enables you to create your own custom types by grouping variables of different types, methods and events. A class is like a blueprint. It defines the data and behavior of a type. If the class is not declared as static, user can use it by creating objects or instances which are assigned to a variable.
Declaring Classes
- Classes are declared by using the class keyword,
public class Customer{ //Fields, properties, methods and events go here...}
Note: Access modifier specify the access rules for the members as well as the class itself, if not mentioned then the default access specifier for a class type is internal. Default access for the members is private.
The possible access modifiers for the class (and for members of the class) are:
• public • private • protected • internal • protected internal
- Public access means that the class can be seen by any method of any class.
- Private access means that the class (or method) can only be seen by methods of the class in which it is created.
- Protected is just like private except that the protected member is visible to classes derived from this one
- Internal access is like private except that the internal members are visible to any class in this assembly. The assembly is the basic unit of creating a program
- Protected internal is the union of both internal and protected: that is the class (or method) is private except to derived classes and also classes in the current assembly.
Constructors
A constructor is a special method of a class, used to create a valid instance of the class. The constructor must have the same name as the class itself. Constructors have no return value, not even void. Their purpose is to create a valid instance, and they are called by the compiler; your code never explicitly calls a constructor.
Default Constructor
- If you create a class with no constructor at all, the compiler will provide a default constructor for you, that does nothing.
- It is easy to be confused about the meaning of default a constructor. To be clear, the default constructor is any constructor that takes no parameters, whether you create it explicitly or it is provided by the compiler.
- Notice that you are able to create your own default constructor, in which case one will not be provided for you; if you create any constructors at all you will not get a default constructor from the compiler. The default constructor that you create may do work.
Broadly speaking, a constructor is a method in the class which gets executed when its object is created. Usually, we put the initialization code in the constructor.
public class mySample
{
public mySampl ()
{
// This is the constructor method.
}
// rest of the class members goes here.
}
Constructor Overloading
C# supports overloading of constructors, that means, we can have constructors with different sets of parameters.
public class mySampleClass
{
public mySampleClass()
{
// This is the no parameter constructor method.
// First Constructor
}
public mySampleClass(int Age)
{
// This is the constructor with one parameter.
// Second Constructor
}
public mySampleClass(int Age, string Name)
{
// This is the constructor with two parameters.
// Third Constructor
}
// rest of the class members goes here.
}
Calling the constructor now depends on the way you instantiate the object. For example:
mySample obj = new mySampleClass() // At this time the code of no parameter // constructor (First Constructor)will be executed
mySample obj = new mySampleClass(12) // At this time the code of one parameter // constructor(Second Constructor)will be executed.
The this Keyword
The this keyword refers to the current object. Within a constructor, the this keyword refers to the object being created. It is common to name private member variables with the same name as the parameters passed to the constructor; in such a case you can use the this keyword to differentiate the member variable from the parameter
class Person
{
//Field "name" in class Person
string name = "ali";
//Constructor of the Person class, takes the name of the Person as argument
public void Person (string name)
{
//Assign the value of the constructor argument "name" to the field "name"
this.name = name;
Console.WriteLine(this.name);
}
}
Post a Comment