static and Instance Members in c sharp

The fields, properties, and methods of a class can be either instance members or static members. Instance members are associated with instances of a type, while static members are associated with the class and not with any particular instance. Methods are instance methods unless you explicitly mark them with the keyword static.

The vast majority of methods will be instance methods. The semantics of an instance method are that you are taking an action on a specific object. From time to time, however, it is convenient to be able to invoke a method without having an instance of the class, and for that you will use a static method.

You access a static member through the name of the class in which it is declared. For example, suppose you have a class named Button and have instantiated objects of that class named btnUpdate and btnDelete.

Suppose that the Button class has an instance method Draw() and a static method GetButtonCount(). The job of Draw() is to draw the current button, and the job of GetButtonCount is to return the number of buttons currently visible on the form.

You access an instance method through an instance of the class — that is, through an object:

btnUpdate.SomeMethod();

You access a static method through the class name, not through an instance:

Button.GetButtonCount();


Invoking Static Methods

Static methods are said to operate on the class, rather than on an instance of the class. They do not have a this reference, as there is no instance to point to.

Static methods cannot directly access nonstatic members. You will remember that Main() is marked static. For Main() to call a nonstatic method of any class, including its own class, it must instantiate an object.

For the next example, use Visual Studio .NET to create a new console application named StaticTester. VS.NET creates a namespace StaticTester and a class named Class1. Rename Class1 to Tester. Get rid of all the comments and the attribute [STATThread] that Visual Studio .NET puts above Main(). Delete the args parameter to Main(). When you are done, your source code should look like this:


using System;

namespace StaticTester

{

class Tester

{

static void Main()

{

}

}

}

That is a good starting point. Until now, you've always done all the work of the program right in the Main() method, but now you'll create an instance method, Run(). The work of the program will now be done in the Run() method, rather than in the Main() method.

Within the class, but not within the Main() method, declare a new instance method named Run(). When you declare a method you write the accessor (public), followed by the return type, the identifier, and then parentheses.

public void Run()

The parentheses will hold parameters, but Run() won't have any parameters, so you can just leave the parentheses empty. Create braces for the method, and within the braces, just print Hello World to the console.

public void Run()

{

Console.WriteLine("Hello world");



}

Run() is an instance method. Main() is a static method and cannot invoke Run() directly. You will therefore create an instance of the Tester class and call Run() on that instance:

Tester t = new Tester();

When you type the keyword new, IntelliSense tries to help you with the class name. You'll find that Tester is in the list; it is a legitimate class like any other.

(8.7.1)

RELATED POST
VISUAL STUDIO INTRODUCTION

C SHARP INTRODUCTION

C SHARP OUT LOOK

DOT NET AND C SHARP

C SHARP APPLICATION STRICTURE

OOPS INTRODUCTION

OOPS AND C SHARP

IDE AND C SHARP

INSTANTIATING OBJECTS IN C SHARP

CLASSES AND OBJECTS IN C SHARP

OPERATORS IN C SHARP

SWITCH AND ITERATION IN C SHARP

BRANCHING IN C SHARP

CONSTANTS AND STRING

STATIC AND INSTANCE MEMBERS IN DOT NET

No comments:

Post a Comment