Class in C#.Net
In C#.NET a class is a user defined Datatype and is known as Reference Type.
- A class can contain following members in C#.NET
- Data Fields
1. Diagrammatic representation of a class:-
A class is represented using rectangle symbol divided in to three parts
General notation in UML
2. Data Fields
- It’s used to store the data related to the class.
- Except Data Fields no other member of a class can store the data.
- To declare any Data Field, we use the following
Syntax: Access modifier Datatype Data Field Name [=Initilising Value];
Ex: Public int Emp_id; String E_Name; Double salary;
- Access modifier can be any one of
i) Private ii) Protected iii) Internal iv) ProtectedInternal v) Public
- Default accessibility of a class is internal
- Any Programming language will have methods.
- A method is a re-usable piece of code which can be called again and again and used to perform required tasks.
- In any Programming language a method can be of two types:
(i) Procedures ii) Functions
- A procedures is a method which doesn’t return any value to the calling place.
- A function is a method which always will return single value to the calling place.
- As a function will return some value to the calling place, mentioning return type is compulsory if function does not return any value then mention return type as void.
Imp Note: C#.Net will support only functions and doesn’t support procedures, where as VB.Net will support both function and procedures.
Syntax to create a class: Access Modifier class Name
Syntax to create an object: Class Name Object Name = new Class Name ([args list])
here, class name is constructor name
- To access any members of a class, with the help of object we will use. (dot) operator known as Member Access Operator like Objectname. Member Name
- Using member access operator, we can access any member based on its accessibility.
- When an object to a class is created, runtime will allot an memory for the members of a class and reference will be maintain from the object, this will be done by new_Keyword / Operator.
- In the above example, memory will be allotted fr the members and reference will be maintained from the objects kike…., (Fig Required)
- If new Keyword is not used, then memory will not be allotted to the Data Fields and no reference will be maintained from the object and raises an error as Null Reference Error.
- this Keyword: this is a keyword used to access the current class in the same class.
- this is an object for the current class in the same class.
- How Encapsulation is Implemented:
- In the above example when Obj1.GetEmpData() function is called Obj 1 reference will be copied in to this keyword / Object and Obj 1referenced Data Fields will be bound to GetEmpData() function.
- When Obj2.GetEmpData () function is called Ogj2 reference will be copied in to this keyword / Object and Obj2 referenced Data Fields will be bound to GetEmpData() function.
- Similarly for DisplayEmpData() function also, i.e. when ever any function is called with any Object of the class than Object referenced Data Fields(Member Variables) will be bound to the respective member Function, this feature we call as Encapsulation.