C# Classes and Objects

What is class and object?

A class defines a type of object, but it is not an object itself. An object is a concrete entity based on a class and is sometimes referred to as an instance of a class. Objects can be created by using the new keyword followed by the name of the class that the object will be based on.

Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestingConsoleApplication
{
    class A     //creating A class
    {
        private int i;  //this is data member of A class
        public void Setdata(int gettinginput)     //this is member function to set the value into data member
        {
            this.i = gettinginput;    //this keyword always refers to the object
        }
        public void Showdata()       //showing the data member value
        {
            Console.WriteLine("I = "+this.i);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            A a1 = new A();   //creating A class's object
            a1.Setdata(100);  //Setdata() function called
            a1.Showdata();    //Showdata() function called
            Console.ReadLine();
        }
    }
}

Output

I = 100


Write a comment


Name Email Message