Add



Class and Object

Class is derived from System.Object. Moreover, all classes are reference type.

Class

Class is type/template. For example, Student

    public class Student
    {
    }

Object

An object is an instance of the class. For example, Ali (Ali is a Student)

Properties

Properties are the characteristics of an object.

Oldest technique to write properties is as follows:

    private int id;
    public int GetId() {return id;}
    public int SetId() {id=value;}

New technique to write properties is as follows:

    private int id;
    public int Id
    {
	    get
        {
	        return id;	
        }
        set 
        {
	        id=value;
        }
    }

Latest technique to write properties is as follows:

    public int Id {get;set;} 

Note that Read-only property is such property which has no setter.

Functions / Methods / Actions

What an object will perform.

Constant

We cannot change value of a constant during the runtime. For example,

    const int a = 10;

Variable

We can change value of a variable.

Constructor

Constructor is used for object creation. When you are going to construct the object then which things you want to initialize. Constructor is a special method whose name is same as class name. We do not mention any return type when writing Constructor signature. Constructor returns object of that class (after creation). By default, there is default constructor.

Destructor

Destructor’s name is same as class name but ~ sign is placed at the start of name. .Net does not recommend to write code in destructor because Garbage Collector does it itself the work.

Inner Class

We can define class within a class as well. For example,

    public class OuterClass
    {
	    public OuterClass()
	    {
        }

        class InnerClass
        {
	        public InnerClass()
	        {
            }
        }
    }

Now object of this inner class will be created in following way:

    OuterClass.InnerClass obj = new OuterClass.InnerClass();

Inheritance

The purpose of inheritance is reusability. For example,

    public class MyBaseEntity
    {
	    public int Id {get;set;}
	    public string GetMessage()
	    {
		    return “Welcome to omerjaved.com”;
        }
    }

Sealed Class

If we used sealed keyword before class keyword then no class can inherit from this class.

Abstract Class

If we used abstract keyword before class keyword then object of this class cannot be created.