Static variable and static function in C# Class

Sourav.Kayal
Posted by in C# category on for Beginner level | Points: 250 | Views : 3366 red flag

Static variable, Static function ,C#

Static variable and static function in C# Class

In this article I am going to show you how to implement static variable and static data member in c# class.

Static is a keyword of C# which means constant or not changeable. Now in a class we can define static to both data member and function member. We will see how we can define static keyword both for data and function.

Static data member in C#.

If we define a data member as a static member then the data member is not belong to any particular object , It is common to all object. In below I have given example how to define data member as static member.

namespace Test1
{
    class Test
    {
       public static Int32 Value = 100;
    }
   
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Test.Value);
            Console.ReadLine();
        }
    }
}

As I told ,Static variable does not belong to any object so by object we cannot call it . Then how we will call a static variable?. Using class name. In above example I have defined a class called test and have define a static variable (Value) . Then if you observer body of Main() function you can find within Consolw.WriteLine() function I am calling this static variable using class name like Test.Value.

Static member function in C#

Like static variable we can define function as static and if we define function as static then the function will not be property of any object directly by class name we can call that function. 

namespace Test1
{
    class Test
    {
        public static void Hello()
        {
            Console.WriteLine("I am static function");
        }
    }
   
    class Program
    {
        static void Main(string[] args)
        {
            Test.Hello();
            Console.ReadLine();
        }
    }
}

In the above example I have created one class called Test and have defined one function called Hello() as

static function. If you observer body of main function then you will find I am invoking the static function

directly by class name and not by object of that class. Like below

            Test.Hello();

Can Static function handle non static data?

Answer is no. Static method can work with only static data member. In below I have given one example to show that.

namespace Test1
{
    class Test
    {
        public Int32 Value;
        static void Show()
        {
            Value = 100;  //Error ,because static function cannot handle
                          //non static data 
        }
    }
   
    class Program
    {
        static void Main(string[] args)
        {
           
            Console.ReadLine();
        }
    }
}

Here there is error when I am trying to assign some value within static function to a not static variable.

Can non static function handle static variable ?

Answer is yes. A non static function can able to handle static data like below

namespace Test1
{
    class Test
    {
        public static Int32 Value;
        public void Show()
        {
            Value = 100;
            Console.WriteLine(Value);
        }
    }
   
    class Program
    {
        static void Main(string[] args)
        {
            Test t = new Test();
            t.Show();
            Console.ReadLine();
        }
    }
}

Here Value is static data member and Show() function is non static function. But we can easily call Show() function which containing a static data



Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)