Wednesday 21 October 2015

Classes in C#

CLASS

A class is a construct that enables you to create your own custom types by grouping together variables of other 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, client code can use it by creating objects or instances which are assigned to a variable. The variable remains in memory until all references to it go out of scope. At that time, the CLR marks it as eligible for garbage collection. If the class is declared as static, then only one copy exists in memory and client code can only access it through the class itself, not an instance variable.

Defining Classes


The first line of code defines the code block as a class definition using the keyword Class followed by the name of the class. The body of the class definition is enclosed by an open and closing curly bracket. The code block is structured like this:

class Employee
 {
 }

Creating Class Properties


After defining the starting and ending point of the class code block, the next step is to define the instance variables (often referred to as fields) contained in the class. These variables hold the data that an instance of your class will manipulate. The Private keyword ensures that these instance variables can be manipulated only by the code inside the class. Here are the instance variable definitions:

private int _empID;
private string _loginName;
private string _password;
private string _department;
private string _name;

When a user of the class (client code) needs to query or set the value of these instance variables, public properties are exposed to them. Inside the property block of code are a Get block and a Set block. The Get block returns the value of the private instance variable to the user of the class. This code provides a readable property. The Set block provides a write-enabled property; it passes a value sent in by the client code to the corresponding private instance variable. Here is an example of a property block:

public string Name
{
    get { return _name; }
    set { _name = value; }
}

There may be times when you want to restrict access to a property so that client code can read the property value but not change it. By eliminating the Set block inside the Property block, you create a read-only property. The following code shows how to make the EmployeeID property read-only:

public int EmployeeID
{
    get { return _empID; }
}


The following are some of the benefits of encapsulating the data in this way:

  • Preventing unauthorized access to the data.
  • Ensuring data integrity through error checking.
  • Creating read-only or write-only properties.
  • Isolating users of the class from changes in the implementation code.

Abstract Class:


An Abstract Class means that, no object of this class can be instantiated, but can make derivation of this. It can serve the purpose of base class only as no object of this class can be created.
Abstract Class is denoted by the keyword abstract.

Example:


abstract class myClass
{
public myClass()
{
 // code to initialize the class…
}
abstract public void anyMethod_01();
abstract public void anyMethod_02(int anyVariable);
abstract public int anyMethod_03 (int anyvariable);
}

It is important here to note that abstract classes can have non-abstract method(s), even can have only non-abstract method(s).

Partial Class:


This special type of class called "Partial Class" is introduced with .Net Framework 2.0. Partial Class allows its members – method, properties, and events – to be divided into multiple source files (.cs). At compile time these files get combined into a single class.
Partial Class is denoted by the keyword partial.

Some do's and don'ts about partial class:-


  • All the parts of a partial class must be prefixed with the keyword partial.
  • Accessibility, signature etc. must be same in all parts of the partial class.
  • You cannot sealed one part of the partial class. In that case entire class in sealed.
  • If you define any part of the partial class abstract, entire class will become abstract. 
  • Inheritance cannot be applied to a part of partial class. If you do so, it applies to entire class. 

Example:


public partial class myPartialClass
{
public void firstMethod()
{
// code…
}
}
public partial class myPartialClass
{
public void secondMethod()
{
// code…
}
}

Sealed Class:


A sealed class is a class which cannot be inherited. A sealed class cannot be a base class. The modifier abstract cannot be applied to a sealed class. By default, struct (structure) is sealed. It is the last class in hierarchy. To access the members of a sealed class, you must create objects of that class. 
Sealed Class is denoted by the keyword sealed.

Example:


sealed class mySealedClass
{
int a;
int b;
}
Class mainClass
{
public static void Main()
{
mySealedClass obj = new mySealedClass();
obj.a = 5;
obj.b = 7;
Console.WriteLine("a = {0}, b = {1}", obj.a, obj.b);
}
}

Static Class:


A Static Class is one which cannot be instantiated. The keyword new cannot be used with static classes as members of such class can be called directly by using the class name itself.

Following are the main characteristics of a static class:-


  • A Static Class can only have static members.
  • A Static Class cannot be instantiated. 
  • A Static Class is sealed, so cannot be inherited.
  • A Static Class cannot have a constructor (except static constructor).

Static Class is denoted by the keyword static.


Example:


// static class definition…
public static class myclass
{
public static int addNumbers(int a, int b)
{
return (a + b);
}
}
// to use it, we call directly on the class…
Console.WriteLine("The addition of 5 and 7 is: " + myClass.addNumbers(5, 7));

Related Articles : 






No comments:

Post a Comment