Friday 27 May 2016

Class

class

C# is familiar for OOP (Object Oriented Programming).  Class is the example of it.

Class is a group of similar methods and variables. It includes constant, fields, properties, methods, indexers, events, operators, instance constructors, static constructor, destructors and nested type declaration.

Defining a Class:

<access specifier> class class_name 
{ 
// member variables 
<access specifier> <data type> variable1; 
<access specifier> <data type> variable2; 
... 
<access specifier> <data type> variableN; 
// member methods 
<access specifier> <return type> method1(parameter_list) 
{ 
// method body 
} 
<access specifier> <return type> method2(parameter_list) 
{ 
// method body 
} 
... 
} 

Fields:
A field represents a variable of a class. It can be accessed by class instances.


class ClassName

{

public static string variableName;

public void ClassMethod(string strName)

{

variableName = strName;

}

}
Read-only:
You can define field members as read only (means the field can only be assigned in the declaration or in the constructor of class)

Constant:
Using const keyword you can declare const field of class.
public const string Name = "Sample";
A constant member represents a constant value throughout the program.  It should be assigned in the declaration.
A constant value cannot be reassigned anywhere once declare.
Constant member variable cannot declare as static. It throw error that the constant cannot be marked static.
public static const string Name = "Sample";
Error: The constant 'myClass.Name' cannot be marked static

Method:
A method is a member of class that implements some functionality.
Method implements some action that can be performed by an object.
Method can return a value have a list of parameters and can be accessed through the class.
Static Methods:
Static methods are accessed through the class means you can access using class name instead of declaring object of class.
myClass.methodname(); // you need not declare object to access static mehtod
Non-Static methods:
Non-static methods are accessed through the instance of the class.
myClass cls = new myClass();
cls.methodName(); 
Method in c# support function overloading.

Properties:
Provide access to a class protected fields. It is useful for exposing fields in components.

Events:
It is used to provide notification.

Constants Fields:
Represent a constant value.

Operators:
It is used to define an expression like (+, *, ->, ++, [], and so on).
C# support two types of constructor:

Instance Constructor:
Instance constructors are called every time a class is initialized.
It is like a method which has no return type even void. It can be used to call during initialization of an object.  


public myClass()
{
        Console.WriteLine("Instantiate...");
}
Static Constructor:
Static constructors are executed only once.
Static constructors are used for initialing the values of static variable.
It is called automatically when object of class is instantiate at runtime.

static myClass()
{
        Console.WriteLine("Instantiate...");
}
Other types of constructor
1. Default Constructor
2. Constructor Overloading
3. Private Constructors
4. Constructor Chaining
5. Static Constructors
6. Destructors
C# class does not end with semicolon (;) as c++.
Access specifiers specify the access rules for the member and class. By default access specifiers for class type is internal and data access for the members is private.
Data type specifies the type of variable whereas return type specifies the data type of the data the method returns, if any.
To access class members, you can use with dot (.) operator. Dot operator links the name of an object with the name of a member.
It is not mandatory to declare destructors. It’s called automatically by CLR.
you can also visit:  www.mybook-lang.com

No comments:

Post a Comment