Friday, 28 December 2012 14:54

Static Members in c#

Written by 
Rate this item
(0 votes)

A c# class can have both static and non static members.
static members is declared with the keyword static.

static members used or belong to the class in which
it was declared rather than to objects of class.

data fields,member functions,events,properties are
declared as static or non static.

static field example -

class TestClass
{
public static int first;
public static int second = 20;
}

Static member function example -

inside a c# class member functions can be defined
as static which can access only static members and
non static members by an instance of class.

class TestClass
{
private static int fi = 20;
private static int se = 40;
public static void Method()
{
Console.WriteLine("{0},{1}",fi,se);
}
}

class My{ public static void Main(){ My.Method(); }}

Static properties example -

class MyClass
{
public static int X
{
get{ Console.Write("GET");return 10; }
set{ Console.Write("SET"); }}
}

Read 2668 times
Super User

Email This email address is being protected from spambots. You need JavaScript enabled to view it.

Latest discussions

  • No posts to display.