Search code examples
c#variablesinstance-variables

What is instance based member


in here I want to know what is instance based member? what I think is instance based member == instance variable. Am I correct? If I'm correct, then how can I know which is variable or instance variable? The variable which under constructor that will become to instance variable, right? Or did I misunderstand?


Solution

  • An instance member is essentially anything within a class that is not marked as static. That is, that it can only be used after an instance of the class has been made (with the new keyword). This is because instance members belong to the object, whereas static members belong to the class.

    Members include fields, properties, methods etc.

    For example:

    class Example
    {
      public static int Value1 { get; set; } // Static property
    
      public int Value2 { get; set; } // Instance property
    
      public static string Hello() // Static method
      {
        return "Hello";
      }
    
      public string World() // Instance method
      {
        return " World";
      }
    }
    
    Console.WriteLine(Example.Hello() + new Example().World()); // "Hello World"