Blog
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    APIs
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog
Report Abuse Form
Reason:    
 

Home >> C# >> Static Member
Static Member

Concept of static members
Views: 690     Comments: 0
by Uttam Misra on 10/7/2010

Static Member

A C# class can contain both static and non-static members.When we declare a member with the help of the keyword static, it becomes astatic member. A static member belongs to the class rather than to the objectsof the class. Hence static members are also known as class members andnon-static members are knows as instance members.

Static Field:

Static field can be declared as follows by using the keywordstatic.

Class myclass

{

Public static int x;

Public static int y=20;

}

When we declare a static field inside a class, it can beinitialized with a value .All un-initialized static fields automatically getinitialized to their default values when the class is loaded first time.

The C# provides a special type of constructor knows asstatic constructor to initialize the static data member when the class isloaded at first time. Other static member functions, static constructor cannotaccess non-static members directly.

The name of a static constructor must be the name of theclass and even they don't have any return type. The static constructor cannottake any arguments. That means there is only one form of static constructor,without any arguments. It is not possible to overload a static constructor.

Static Member Function:

Inside a C# class, member functions can be declared asstatic. But a static member function can access only other static members. Theycan access non-static members only through an instance of class.

Example:

Class myclass

{

Private static int x=20;

Private static int y=40;

Public static void method()

{

Console.WriteLine(“{0},{1}”,x,y);

}

}

Class myclint

{

Public static void main()

{

Myclass.method();

}

}

Title :  
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
       
 
Report Abuse Form
Reason:    
 
Latest ArticleRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest Blogs by Uttam MisraRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 2567
  
Copyright © 2013MindStick. All Rights Reserved.