shared and new methods

Posted by Krishnamanohar under C# on 4/22/2013 | Points: 10 | Views : 737 | Status : [Member] | Replies : 2
Hi Experts,
in which situation we use new keyword in methods in which situation we use Shared keyword in methods?
Thanks in Advance
Krish




Responses

Posted by: Satyapriyanayak on: 4/22/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
http://msdn.microsoft.com/en-in/library/51y09td4%28v=vs.71%29.aspx
http://www.dotnetheaven.com/article/use-shared-keyword-in-vb.net

If this post helps you mark it as answer
Thanks

Krishnamanohar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rasik on: 5/13/2013 [Member] Starter | Points: 25

Up
0
Down
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

Login to post response