Static method(shared in VB)
Static methods have no instances. They are called with the type name, not an instance identifier. They are slightly faster than instance methods because of this. Static methods can be public or private.
Static methods cannot access non-static class level members and do not have a 'this' pointer. Instance methods can access those members, but must be called through an instantiated object. This adds another level of indirection.
class Program
{
static void MethodA()
{
Console.WriteLine("Static method");
}
void MethodB()
{
Console.WriteLine("Instance method");
}
static void Main()
{
// Call the static method on the Program type.
Program.MethodA();
// Create a new Program instance and call the instance method.
Program programInstance = new Program();
programInstance.MethodB();
}
}
}
New method :
Use the new modifier to explicitly hide a member inherited from a base class. To hide an inherited member, declare it in the derived class using the same name, and modify it with the new modifier.
If you have a base class:
public class BaseClass
{
public void DoSomething() { }
}
And then the derived class:
public class DerivedType
{
public new void DoSomething() {}
}
If you declare a type of DerivedType and then cast it, the method DoSomething() isn't polymorphic, it will call the base class' method, not the derived one.
BaseClass t = new DerivedType();
t.DoSomething();// Calls the "DoSomething()" method of the base class.
Krishnamanohar, if this helps please login to Mark As Answer. | Alert Moderator