1.

What are class instances in C#?

Answer»

An object of a class is a class instance in C#. All the members of the class can be accessed USING the object or instance of the class.

The definition of the class instance starts with the class name that is followed by the name of the instance. Then the new OPERATOR is used to CREATE the new instance.

A program that demonstrates a class instance in C# is given as FOLLOWS:

using System; namespace Demo {   class Sum   {     private int x;     private int y;     public void value(int val1, int val2)     {         x = val1;         y = val2;     }     public int returnSum()     {         return x + y;     }    }    class Test    {      static void Main(string[] args)      {         Sum obj = new Sum();            obj.value(8, 5);         Console.WriteLine("The sum of 8 and 5 is: {0}" , obj.returnSum());      }    } }

The output of the above program is given as follows:

The sum of 8 and 5 is: 13


Discussion

No Comment Found