By 22/07/2015 18 Comments

Object Creation In C#

  • The object is a thing which is a real creation or implementation of a class (means physical creation of a class), which has physical existence.
  • It is an instance of a class.
  • Pro-grammatically an object is a block of memory which has been allocated and configured according to a class during run-time.
  • According to above Mark sheet class, when we create its object it means we are creating or implementing an actual or real mark sheet object which will have physical reality.
  • We can create multiple different types of objects from that Mark sheet class or multiple Mark sheets can be created from same Mark sheet template or blueprint, each Mark sheet will have its own information, attributes and behavior.

General form of an Object creation:

ClassName refVariable = new ClassName();

Suppose, a Mark sheet object can be created:

MarkSheet markSheet = new MarkSheet();

STATEMENT DESCRIPTION
MarkSheet This is the Mark sheet class name which can be described as “It is a user defined type (a class type) declared”.
markSheet Declare a reference variable of user defined type (MarkSheet) which is used to hold the reference (heap memory addresses) returned by the object MarkSheet (memory block) after creation.
= The assignment operator which assigns the reference (memory address from right to left) to reference variable (markSheet).
new MarkSheet(); The new operator dynamically allocates a block of memory space (at run time) for an object and returns a reference to it, at this point the actual object (MarkSheet) is created on heap memory.
MarkSheet(); This statement defines the constructor for that class (MarkSheet).


Example – 1, a complete Marksheet class:

using System;
namespace csharpBasic
{

    // Declare a class MarkSheet.
    class MarkSheet
    {
        /* Following is the initialization of string and double type class attributes/properties, 
           here these attributes are called fields. 
         */
        public string StudentName = "abc";
        public string StudentAddress = "xyz";
        public string Asp = "Asp.net";
        public string Javascript = "JavaScript";

        public double AspMarks = 80;
        public double JavascriptMarks = 60;

    }    // End class definition scope.

    class Program
    {
        // Static main method void type declaration.
        static void Main(string[] args)
        {
            // An object of a Marksheet class is being declared.
            MarkSheet markSheet = new MarkSheet();

            /* Following statements print the different kind of information of a 
               Marksheet by using the reference variable of type MarkSheet class followed by dot operator (.), 
               dot operator links a class member with a reference variable.
             */
            Console.WriteLine("Student name: {0}", markSheet.StudentName);
            Console.WriteLine("Student address: {0}", markSheet.StudentAddress);

            Console.WriteLine("Subject 1: {0}", markSheet.Asp);
            Console.WriteLine("Asp.net Marks: {0}", markSheet.AspMarks);

            Console.WriteLine("Subject 2: {0}", markSheet.Javascript);
            Console.WriteLine("Javascript Marks: {0}", markSheet.JavascriptMarks);

            Console.ReadKey();
        } 
    }
}


Example – 2, the above program can also be written as following:

using System;
namespace csharpBasic
{

    // Declare a class MarkSheet.
    class MarkSheet
    {
        /* Notice, in this example we do not initialize class fields in this class,
           we just declared them.
         */
        public string StudentName;
        public string StudentAddress;
        public string Asp;
        public string Javascript;
        public double AspMarks;
        public double JavascriptMarks;


    }    // End class definition scope.

    class Program
    {
        // Static main method void type declaration.
        static void Main(string[] args)
        {
            // An object of a Marksheet class is being declared.
            MarkSheet markSheet = new MarkSheet();

            /* Notice, following class fields are initialize in main method (not in class itself) 
               with different values. 
             */
            markSheet.StudentName = "abc";
            markSheet.StudentAddress = "xyz";

            markSheet.Asp = "Asp.net";
            markSheet.AspMarks = 80;

            markSheet.Javascript = "JavaScript";
            markSheet.JavascriptMarks = 60;

            // Following statements print the different kind of Marksheet information.
            Console.WriteLine("Student name: {0}", markSheet.StudentName);
            Console.WriteLine("Student address: {0}", markSheet.StudentAddress);
            Console.WriteLine("Subject 1: {0}", markSheet.Asp);
            Console.WriteLine("Asp.net Marks: {0}", markSheet.AspMarks);
            Console.WriteLine("Subject 2: {0}", markSheet.Javascript);
            Console.WriteLine("Javascript Marks: {0}", markSheet.JavascriptMarks);
            Console.ReadKey();
        } 
    }
}


REMEMBER:

  • In above program examples we define a class in same file but you can also define a class in a separate class file in visual studio (it is recommended).





Next Tutorial →


← Previous Tutorial


Posted in: C# Basics, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .