Breaking News
Loading...
Friday 29 March 2013

How to use Static keyword in .Net?, What is Static keyword in c.Net?

  • Static is keyword. You can declare variable, method and class to be static, when you declare variable to be static then it is shares among all the object of this class as show in example.
  • When you declare any variable or method to be static then you have no need to create class of this you can access directly by its name like "ClassName.VariableName"
  • When you declare any class to be static then this class only contains static member and static methods.
  • When you declare static method then it only use static variable (member) of this class.
Example:

namespace CA_Static
{
     class student
    {
        // define static variable that is shared
        private static int member=0;

       
        public  void increment()
        {
            member++;
        }
        public static int getmember()
        {
            return member;
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            student objstd = new student();
            objstd.increment();

            student objstd1 = new student();
            objstd1.increment();

            student objstd2 = new student();
            objstd2.increment();

            Console.WriteLine(student.getmember());
        }
    }
}


Output:

3

See Also :
  Follow Me On Facebook

--
/\/ir@\/  <(.'.)>

0 comments:

Post a Comment

Thanks for comment

 
Toggle Footer