locked
Whats the difference between class variable & instance variable? & How many variable types r there? RRS feed

  • Question

  • C#:Whats the difference between class variable & instance variable? & How many variable types r there in C#.net?
    ---Sri's
    Friday, June 18, 2010 5:28 PM

Answers

  • Class Variables are variables which are declared as static in Class. And Instance variable are variables which are not declared as static they can either private or public.

    Static Example

    Declaring instance variables:

    private string customerName;
    private int customerAge;
    private boolean favoriteCustomer = false;

    Declaring a class variable:

    private static int noOfCustomers;

     


    Regards
    Nayan Paregi (MCTS)
    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Friday, June 18, 2010 5:38 PM
  • "Class variables", in C#, are called static variables.  They are variables that are defined at the type level instead of defined on an instance.  This allows all instances of the class (and the class itself) to reference the same variable.

     

    Instance variables are defined on an instance of the class.  This means that each instance of an object will have it's own "copy" of the instance variable.

     

    For example:

     

    // Given:
    
    public class MyClass {
    
      public static int ClassVariable { get; set; }
    
      public int InstanceVariable { get; set; }
    
      public void Print()
      {
         Console.WriteLine("Class: {0} - Instance: {1}", ClassVaraible, InstanceVariable );
      }
    }
    
    
    // You can do:
    
    // Set the "class" level variable
    MyClass.ClassVariable = 3;
    
    // Create two instances
    MyClass instance1 = new MyClass();
    MyClass instance2 = new MyClass();
    
    instance1.InstanceVariable = 5;
    instance2.InstanceVariable = 7;
    
    instance1.Print(); // Prints 3, 5
    
    instance2.Print(); // Prints 3, 7
    


    Reed Copsey, Jr. - http://reedcopsey.com
    Friday, June 18, 2010 5:42 PM
    Moderator
  • I think people use different terminology for these kind of things like we hear all kind of terms Base class, parent class, inherited class or derived class etc.

    As per the ECMA Specification of C# here is some explaination of fields:

    A field is a member that represents a variable associated with an object or class.

    A static variable is associated with a type, whereas an instance variable is associated with an instance.

    For example:

    class Employee
    {
         private static ds;
         public string Name;
         public decimal Salary;

    }

    Shows an Employee class that has a private static variable and two public instance variables.


    Vidya Vrat Agarwal. http://dotnetpassion.blogspot,com
    Friday, June 18, 2010 5:43 PM

All replies

  • Class Variables are variables which are declared as static in Class. And Instance variable are variables which are not declared as static they can either private or public.

    Static Example

    Declaring instance variables:

    private string customerName;
    private int customerAge;
    private boolean favoriteCustomer = false;

    Declaring a class variable:

    private static int noOfCustomers;

     


    Regards
    Nayan Paregi (MCTS)
    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Friday, June 18, 2010 5:38 PM
  • "Class variables", in C#, are called static variables.  They are variables that are defined at the type level instead of defined on an instance.  This allows all instances of the class (and the class itself) to reference the same variable.

     

    Instance variables are defined on an instance of the class.  This means that each instance of an object will have it's own "copy" of the instance variable.

     

    For example:

     

    // Given:
    
    public class MyClass {
    
      public static int ClassVariable { get; set; }
    
      public int InstanceVariable { get; set; }
    
      public void Print()
      {
         Console.WriteLine("Class: {0} - Instance: {1}", ClassVaraible, InstanceVariable );
      }
    }
    
    
    // You can do:
    
    // Set the "class" level variable
    MyClass.ClassVariable = 3;
    
    // Create two instances
    MyClass instance1 = new MyClass();
    MyClass instance2 = new MyClass();
    
    instance1.InstanceVariable = 5;
    instance2.InstanceVariable = 7;
    
    instance1.Print(); // Prints 3, 5
    
    instance2.Print(); // Prints 3, 7
    


    Reed Copsey, Jr. - http://reedcopsey.com
    Friday, June 18, 2010 5:42 PM
    Moderator
  • I think people use different terminology for these kind of things like we hear all kind of terms Base class, parent class, inherited class or derived class etc.

    As per the ECMA Specification of C# here is some explaination of fields:

    A field is a member that represents a variable associated with an object or class.

    A static variable is associated with a type, whereas an instance variable is associated with an instance.

    For example:

    class Employee
    {
         private static ds;
         public string Name;
         public decimal Salary;

    }

    Shows an Employee class that has a private static variable and two public instance variables.


    Vidya Vrat Agarwal. http://dotnetpassion.blogspot,com
    Friday, June 18, 2010 5:43 PM