Part-10-C# Tutorial Class & object

Class & Object

  • Class are created by using class "Keyword" declaration.A class declaration begin with attributes ,modifiers and accessibility of a class.  
  • Class are the user defined data types that represent the state and behaviour of an object. State represents the properties and behaviour is the action that objects can perform.                                                                                                                              
  • The name of the class ex:- Program,Test,Demo..etc.                                                                            
  • The declaration is followed by the body of the class that contains member definitions between the curly braces “{“ and “}”.  
Syantax :-
public class Program
{
  //code
}
  • Class  can be declared by using the following access specifiers that limit the accessibility of class to other classes, however some classes does not require any access modifiers.
             Five type's of Access Modifiers (to be discuss in later session)
    1. Public
    2. Private
    3. Protected
    4. Internal
    5. Protected Internal
    Some Key points about classes
    • Classes are reference types that hold the object created dynamically in a heap memory.              
    • All classes have a base type of System.Object.                                                             
    • The default access modifier of a class is Internal.                                                           
    • The default access modifier of methods and variables is Private
    The following are types of class in C#:
    Types of class to be discuss later.
    1. Abstract class
    2. Partial class 
    3. Sealed class
    4. Static class  
    Example :-
    Class Employee{
        int empId = 24;
        string empName ="chandan";
        string job = "Software Developer";
    }

    A class can also have methods. A method defines the operations that can be performed inside a class.

        class Demo
        {
          //method
           public void add()
            {
                Console.WriteLine("This is inside class method");
            }

        }
        class Program
        {
            static void Main(string[] args)
            {
                Demo demo = new Demo();
                demo.add();
                Console.Read();
            }
       }
    Thus, in the above program, the Demo class, objects create in the main class As the Demo class contains all method of a base class implicitly, we able to create Object.

    Object :

    An instance of an object is created by using the “new” operator. It is denoted by using the class name for which the instance is being created followed by a variable to store the reference of the instance,  then demo class name Demo with reference name ex-demo “=” equals to sign followed by the “new” keyword and then again the name of the class with both open and closed brackets “()” and end with semicolon.
     Demo demo = new Demo();

    new "keyword" create reference  type, new means memory allocate. 


              
    please comment if you have any doubt  & query regarding to this topic and for another new tutorial topic just drop me a comment.