+1 vote
in C Sharp by
What is Static function and example of in-built Static function in .Net?

1 Answer

0 votes
by
edited by

Has the keyword static.

•      A Static function is one that can be accessed without creating an object of the class.

•      It can be accessed using the name of the class.

•      It can belong to Static as well as Non-Static classes.

•      One cannot use external Non-Static objects and functions inside a Static method.

•      One can use Static function in Static as well as Non-Static classes.

•      Keyword this is not supported.

•      Example of one inbuilt Static function in C# is Convert.ToString().

Example:

public class A{

    public static void C()    {

        //Invalid

        this.D();

    }

    public void D()

    {       //Valid

       C();

    }

}

Non-Static class

•      Does not have the keyword static.

•      Can be instantiated and object can be created.

•      Keyword new is supported.

•      Objects and Functions cannot be accessed by class name unless marked static.

•      Not mandatory for functions and methods to be marked static i.e. can contain Static as well as Non-Static members.

•      Non-Static members can be accessed using this keyword. But Static members cannot be accessed using this keyword.

•      Multiple instances can be created.

Example:

public class A

{

    public object C;

    public static object G;

 

    public static void D()

    { //Invalid

       var p = C;

        //Valid

       var q = G;

       //Invalid

        E();

    }   

}

...