locked
What is static keyword in c#

    Question

  • What are static methods, static datatypes
    in c#,
    can any one give example on it..

    adil
    Tuesday, August 26, 2008 10:44 AM

Answers

  • Static methods When the static keyword is used to declare a class, the member in context must be directly invoked from the class, rather than from the instance. Consider the following example

    //Consider writing the following line of code...
    Console obj = new Console();
    obj.Writeline("Vishal likes static members"); //This line does'nt print

    //This does'nt work, because WriteLine is a static method defined in the class Console
    //The Console class is a static class

    To use static members, give a reference to the exact class, as an instance in this case won't work.

    To make this work, write...
    Console.Writeline("Vishal likes static members");

    To work with members of static classes, no need to create their instances.
    Static Member - A class member declared with the keyword static is a static member. A static member is owned by the class, not by its instances (objects of the class).
    Note that static members are actually class members, while non-static members are instance members (means they are owned by the instances). Both in C# & VB.NET, we may create static/shared events, properties, fields and functions.

    • Marked as answer by adilahmed Tuesday, August 26, 2008 1:57 PM
    Tuesday, August 26, 2008 12:19 PM

All replies

  • Static methods When the static keyword is used to declare a class, the member in context must be directly invoked from the class, rather than from the instance. Consider the following example

    //Consider writing the following line of code...
    Console obj = new Console();
    obj.Writeline("Vishal likes static members"); //This line does'nt print

    //This does'nt work, because WriteLine is a static method defined in the class Console
    //The Console class is a static class

    To use static members, give a reference to the exact class, as an instance in this case won't work.

    To make this work, write...
    Console.Writeline("Vishal likes static members");

    To work with members of static classes, no need to create their instances.
    Static Member - A class member declared with the keyword static is a static member. A static member is owned by the class, not by its instances (objects of the class).
    Note that static members are actually class members, while non-static members are instance members (means they are owned by the instances). Both in C# & VB.NET, we may create static/shared events, properties, fields and functions.

    • Marked as answer by adilahmed Tuesday, August 26, 2008 1:57 PM
    Tuesday, August 26, 2008 12:19 PM
  • ...and
    >>>
        //Consider writing the following line of code... 
        Console obj = new Console();
        obj.Writeline("Vishal likes static members"); //This line does'nt print
    <<<
    This won't even compile.  C# won't let you instance a static class, nor will it let you reference a static member from an instance.

    You can define static classes at the "namespace" scope as well as inside the scope of other classes.  Members of a static class can be initialized.  Consider the following....

        public partial class Form1
        {
            private void button1_Click()
            {
                // do stuff unrelated to the static class

                //... now call the method in the Static classes member MyClass.
                MyConsole.MyClass.Method2();
            }

                public static class MyConsole
                {
                    static MyConsole()
                    {
                        Console.WriteLine("Instance of a Static Class?");
                    }
                    public static void WriteLine() {}
                    public static MyClassWithStatics MyClass = new MyClassWithStatics();
                }
                public class MyClassWithStatics
                {
                    public MyClassWithStatics()
                    {
                        Console.WriteLine("Instantiate MyClassWithStatics");
                    }
                    public static void Method1() { Console.WriteLine("Method1"); }
                    public void Method2() { Console.WriteLine("Method2"); }
                }
        }

    The print statements don't run until the call to Method2() is made.  I.e, the MyConsole static constructor, and the MyClass = new() do not run until the static class is accessed. 


    Les Potter, Xalnix Corporation, Yet Another C# Blog
    Tuesday, August 26, 2008 1:33 PM