Latest web development tutorials

C # class (Class)

When you define a class, you define a blueprint for a data type. This actually does not define any data, but it defines the name of the class what it means, that is, what constitutes an object class and perform what operations on the object. Objects are instances of classes. Methods and variables constituting the class becomes members of the class.

Class definition

The definition of the class is to start, followed by the class name of the keywordclass.Body of the class, contained within a pair of curly braces. The following is the general form of the class definition:

<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 
    }
    ...
    <Access specifier> <return type> methodN (parameter_list) 
    {
        // Method body 
    }
}

Please note:

  • Access Identifier <access specifier> specifies the access rule class and its members. If not specified, the default access identifier. Default Access Identifier class isinternal, the default access identifier members is private.
  • Data type <data type> specifies the type of the variable, return type <return type> specifies the type of data returned by the method returns.
  • If you want to access a member of the class, you want to use dot (.) Operator.
  • Dot operator links the name of the name and the members of the object.

The following examples illustrate the concepts discussed so far:

using System;
namespace BoxApplication
{
    class Box
    {
       public double length; // length public double breadth; // width public double height; // height}
    class Boxtester
    {
        static void Main (string [] args)
        {
            Box Box1 = new Box (); // declare Box1, type Box
            Box Box2 = new Box (); // declare Box2, type Box
            double volume = 0.0; // // Box1 volume detailing Box1.height = 5.0;
            Box1.length = 6.0;
            Box1.breadth = 7.0;

            // Box2 detailing Box2.height = 10.0;
            Box2.length = 12.0;
            Box2.breadth = 13.0;
           
            // Box1 volume volume = Box1.height * Box1.length * Box1.breadth;
            Console.WriteLine ( "Box1 volume: {0}", volume);

            // Box2 volume volume = Box2.height * Box2.length * Box2.breadth;
            Console.WriteLine ( "Box2 volume: {0}", volume);
            Console.ReadKey ();
        }
    }
}

When the above code is compiled and executed, it produces the following results:

Box1 volume: 210
Box2 volume: 1560

Member functions and package

Class member functions it is a definition or function prototype in the class definition, just like any other variable. As a member of the class, it can operate on any object class, all members and can access the object's class.

Member variables are properties of objects (from a design point of view), and they remain private to implement package. These variables can only be used to access the public member functions.

Let's use the above concepts to set and get the value of a different type of class members:

using System;
namespace BoxApplication
{
    class Box
    {
       private double length; // length private double breadth; // width private double height; // height public void setLength (double len)
       {
            length = len;
       }

       public void setBreadth (double bre)
       {
            breadth = bre;
       }

       public void setHeight (double hei)
       {
            height = hei;
       }
       public double getVolume ()
       {
           return length * breadth * height;
       }
    }
    class Boxtester
    {
        static void Main (string [] args)
        {
            Box Box1 = new Box (); // declare Box1, type Box
            Box Box2 = new Box (); // declare Box2, type Box
            double volume; // // Box1 volume detailing Box1.setLength (6.0);
            Box1.setBreadth (7.0);
            Box1.setHeight (5.0);

            // Box2 detailing Box2.setLength (12.0);
            Box2.setBreadth (13.0);
            Box2.setHeight (10.0);
       
            // Box1 volume of volume = Box1.getVolume ();
            Console.WriteLine ( "Box1 volume: {0}", volume);

            // Box2 volume of volume = Box2.getVolume ();
            Console.WriteLine ( "Box2 volume: {0}", volume);
           
            Console.ReadKey ();
        }
    }
}

When the above code is compiled and executed, it produces the following results:

Box1 volume: 210
Box2 volume: 1560

C # constructors

Constructor of the class is a special class member function executed when creating a new object class.

Name The name of the class constructor exactly the same, it has no return type.

The following example illustrates the concept of the constructor:

using System;
namespace LineApplication
{
   class Line
   {
      private double length; // length of the line public Line ()
      {
         Console.WriteLine ( "Object has been created");
      }

      public void setLength (double len)
      {
         length = len;
      }
      public double getLength ()
      {
         return length;
      }

      static void Main (string [] args)
      {
         Line line = new Line ();    
         // Set the line length line.setLength (6.0);
         Console.WriteLine ( "The length of the line: {0}", line.getLength ());
         Console.ReadKey ();
      }
   }
}

When the above code is compiled and executed, it produces the following results:

Created objects line length: 6

The default constructor has no parameters.But if you need a constructor with a parameter can have arguments, this constructor is calledparameterized constructor.This technique can help you create the object while the object to assign initial values, see the following specific examples:

using System;
namespace LineApplication
{
   class Line
   {
      private double length; // length of the line public Line (double len) // parameterized constructor {
         Console.WriteLine ( "object has been created, length = {0}", len);
         length = len;
      }

      public void setLength (double len)
      {
         length = len;
      }
      public double getLength ()
      {
         return length;
      }

      static void Main (string [] args)
      {
         Line line = new Line (10.0);
         Console.WriteLine ( "The length of the line: {0}", line.getLength ()); 
         // Set the line length line.setLength (6.0);
         Console.WriteLine ( "The length of the line: {0}", line.getLength ()); 
         Console.ReadKey ();
      }
   }
}

When the above code is compiled and executed, it produces the following results:

Object has been created, length = 10
Line length: 10
Line length: 6

In C # destructor

Classdestructor is a special class member function, when the object type is out of range.

Name destructor is in front of the name of the class with a wavy (~) as a prefix, it does not return a value, it takes no parameters.

Destructor for the end of the program (such as closing files to free memory, etc.) prior to the release of resources. Destructors can not be inherited or overloaded.

The following example illustrates the concept of the destructor:

using System;
namespace LineApplication
{
   class Line
   {
      private double length; // length of the line public Line () // constructor {
         Console.WriteLine ( "Object has been created");
      }
      ~ Line () // destructor {
         Console.WriteLine ( "Object has been deleted");
      }

      public void setLength (double len)
      {
         length = len;
      }
      public double getLength ()
      {
         return length;
      }

      static void Main (string [] args)
      {
         Line line = new Line ();
         // Set the line length line.setLength (6.0);
         Console.WriteLine ( "The length of the line: {0}", line.getLength ());           
      }
   }
}

When the above code is compiled and executed, it produces the following results:

Created objects line length: 6
Deleted objects 

C # static member class

We can use the keywordstatic class member is declared static.When we declare a class member is static, meaning that no matter how many class object is created, there is only one copy of the static members.

Only one instance of the members of the keywordstatic means that class.Static variables used to define constants, because their value can be called directly without the need to create an instance of the class class to obtain. Static variables can be initialized in the definition of the external member function or class. You can also initialize static variables defined within the class.

The following example demonstrates the use ofstatic variables:

using System;
namespace StaticVarApplication
{
    class StaticVar
    {
       public static int num;
        public void count ()
        {
            num ++;
        }
        public int getNum ()
        {
            return num;
        }
    }
    class StaticTester
    {
        static void Main (string [] args)
        {
            StaticVar s1 = new StaticVar ();
            StaticVar s2 = new StaticVar ();
            s1.count ();
            s1.count ();
            s1.count ();
            s2.count ();
            s2.count ();
            s2.count ();         
            Console.WriteLine ( "s1 variable num: {0}", s1.getNum ());
            Console.WriteLine ( "s2 variable num: {0}", s2.getNum ());
            Console.ReadKey ();
        }
    }
}

When the above code is compiled and executed, it produces the following results:

s1 variable num: 6
s2 variable num: 6

You can also put amember function is declared as static.Such functions can only access static variables. Static function before the object is created, it exists. The following example demonstrates the use ofstatic functions:

using System;
namespace StaticVarApplication
{
    class StaticVar
    {
       public static int num;
        public void count ()
        {
            num ++;
        }
        public static int getNum ()
        {
            return num;
        }
    }
    class StaticTester
    {
        static void Main (string [] args)
        {
            StaticVar s = new StaticVar ();
            s.count ();
            s.count ();
            s.count ();                   
            Console.WriteLine ( "variable num: {0}", StaticVar.getNum ());
            Console.ReadKey ();
        }
    }
}

When the above code is compiled and executed, it produces the following results:

Variable num: 3