Classes in C#

Walden Systems Geeks Corner Classes in C# programming tutorial Rutherford NJ New Jersey NYC New York North Bergen County
C# is an elegant and type-safe object-oriented language that enables developers to build a variety of secure and robust applications that run on the .NET Framework. You can use C# to create Windows client applications, XML Web services, distributed components, client-server applications, database applications, and much, much more. Visual C# provides an advanced code editor, convenient user interface designers, integrated debugger, and many other tools to make it easier to develop applications based on the C# language and the .NET Framework.

A class is like a blueprint for a data type. It defines what the class name means but doesn't define any data. Essentially, we are describing what an object of the class consists of and what operations can be performed on that object. Objects are instances of a class. The methods and variables that constitute a class are called members of the class.

Defining

A class definition starts with the keyword class followed by the class name; and the class body enclosed by a pair of curly braces. Access specifiers gives the access rules for the members as well as the class itself. The default access specifier for a class type is internal if not specified. Default access for the members is private if not specified. The data type specifies the type of variable, and return type specifies the data type of the data the method returns, if none, use void. To access the class members, we use the dot operator. The dot operator links the name of an object with the name of a member.

public class MyClass
{
    public string  myField = string.Empty;

    public MyClass()
    {
    }

    public void MyMethod(int parameter1, string parameter2)
    {
        Console.WriteLine("First Parameter {0}, second parameter {1}", 
                                                    parameter1, parameter2);
    }

    public int MyAutoImplementedProperty { get; set; }

    private int myPropertyVar;
    
    public int MyProperty
    {
            get { return myPropertyVar; }
            set { myPropertyVar = value; }
    } 
}


Member functions and encapsulation

A member function of a class is a function that has its definition or its prototype within the class definition similar to any other variable. It operates on any object of the class of which it is a member, and has access to all the members of a class for that object. Member variables are the attributes of an object and they are kept private to implement encapsulation. These variables can only be accessed using the public member functions.

class Box {
      private double length;  
      private double width; 
      private double height;   
      
      public void setLength( double len ) {
         length = len;
      }
      public void setWidth( double wid ) {
         width = did;
      }
      public void setHeight( double hei ) {
         height = hei;
      }
      public double getVolume() {
         return length * width * height;
      }
   }

The variables length, width, and height aren't accessible outside the class definition so we have to use the setWidth, setLength, setHeight public methods to set them.

Constructors

A class constructor is a special member function of a class that is executed whenever we create new objects of that class. A constructor has exactly the same name as that of class and it does not have any return type. A default constructor does not have any parameter but if you need, a constructor can have parameters. Parameterized constructors helps us to assign initial value to an object at the time of its creation.

class Box {
      private double length;  
      private double width; 
      private double height;

      public Box( double p_len, double p_height, double p_width ) {
         length = p_len ;
	 height = p_height ;
	 width = p_width ;
      }  
      
      public void setLength( double len ) {
         length = len;
      }
      public void setWidth( double wid ) {
         width = did;
      }
      public void setHeight( double hei ) {
         height = hei;
      }
      public double getVolume() {
         return length * width * height;
      }
   }

Destructors

A destructor is a special member function of a class that is executed whenever an object of its class goes out of scope. A destructor has the same name as that of the class with a prefixed tilde and it can neither return a value nor can it take any parameters. Destructor can be very useful for releasing memory resources before exiting the program. Destructors cannot be inherited or overloaded.

class Box {
      private double length;  
      private double width; 
      private double height;

      public Box( double p_len, double p_height, double p_width ) {
         length = p_len ;
	 height = p_height ;
	 width = p_width ;
      }  
    
      ~Box() {
      }
      
      public void setLength( double len ) {
         length = len;
      }
      public void setWidth( double wid ) {
         width = did;
      }
      public void setHeight( double hei ) {
         height = hei;
      }
      public double getVolume() {
         return length * width * height;
      }
   }