New key word with function [Resolved]

Posted by Programmer123 under C# on 9/19/2013 | Points: 10 | Views : 1195 | Status : [Member] | Replies : 7
I have attended one of interview on xyz company. They have asked below question.
How to use new key work with function.
Can any one tell me what is this and how to use this?




Responses

Posted by: Allemahesh on: 9/20/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
You can get clear idea by using below examples:-

Example 1:-


public class Foo
{
public virtual bool DoSomething() { return false; }
}

public class Bar : Foo
{
public override bool DoSomething() { return true; }
}

class Program
{
static void Main(string[] args)
{
Foo test = new Bar();
Console.WriteLine(test.DoSomething());
Console.ReadLine();
}
}


Here you will get output as True

Example 2:-


public class Foo
{
public bool DoSomething() { return false; }
}

public class Bar : Foo
{
public new bool DoSomething() { return true; }
}

class Program
{
static void Main(string[] args)
{
Foo test = new Bar();
Console.WriteLine(test.DoSomething());
Console.ReadLine();
}
}


Here you will get output as False

Example 2:-


public class Foo
{
public virtual bool DoSomething() { return false; }
}

public class Bar : Foo
{
public new bool DoSomething() { return true; }
}

class Program
{
static void Main(string[] args)
{
Foo test = new Bar();
Console.WriteLine(test.DoSomething());
Console.ReadLine();
}
}


Here you will get output as False

So, if you are doing real polymorphism you SHOULD ALWAYS OVERRIDE.
The only place where you need to use "new" is when the method is not related in any way to the base class version.

You can see the image to understand more clearly:- http://farm4.static.flickr.com/3291/2906020424_f11f257afa.jpg?v=0

Happy Coding
If it helps you or directs U towards the solution, MARK IT AS ANSWER

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

Posted by: Allemahesh on: 9/20/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
You can also see the below example:-

class A

{
public void foo()
{
Console.WriteLine("A::foo()");
}
public virtual void bar()
{
Console.WriteLine("A::bar()");
}
}

class B : A
{
public new void foo()
{
Console.WriteLine("B::foo()");
}
public override void bar()
{
Console.WriteLine("B::bar()");
}
}

class Program
{
static int Main(string[] args)
{
B b = new B();
A a = b;
a.foo(); // Prints A::foo
b.foo(); // Prints B::foo
a.bar(); // Prints B::bar
b.bar(); // Prints B::bar
return 0;
}
}


Happy Coding
If it helps you or directs U towards the solution, MARK IT AS ANSWER

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

Posted by: Bandi on: 9/19/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
You cannot override a non-virtual or static method. The overridden base method must be virtual, abstract, or override.So the 'new' keyword is needed to allow you to 'override' non-virtual and static methods.

reference
http://stackoverflow.com/questions/1014295/new-keyword-in-method-signature


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

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

Posted by: Bandi on: 9/19/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
http://msdn.microsoft.com/en-us/library/435f1dw2.aspx
http://msdn.microsoft.com/en-us/library/ms173153.aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

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

Posted by: Bandi on: 9/19/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
new Operator:
Used to create objects and invoke constructors.

new Modifier:
Used to hide an inherited memberfrom a base class member.

new Constraint:
Used to restrict types that might be used as arguments for a type parameter in a generic declaration.

When used as a modifier, thenewkeyword explicitly hides a memberthat's inherited from a base class. When you hide an inherited member, the derived version of the member replaces the base-class version. You can hide members without using thenewmodifier, but the result is a warning. If you usenewto explicitly hide a member, the modifier suppresses this warning and documents the fact that the derived version is intended as a replacement.To hide an inherited member, declare it in the derived class by using the same name, and modify it with thenewmodifier. For example:

public class BaseC{

public int x;
public void Invoke() { }
}
public class DerivedC : BaseC{
new public void Invoke() { }
}

In this example,BaseC.Invokeis hidden by DerivedC.Invoke. The fieldxis not affected because it is not hidden by a similar name.Name hiding through inheritance takes one of the following forms:
*.A constant, field, property, or type introduced in a class or struct hides all base class members with the same name.
*.A method introduced in a class or struct hides properties, fields,and types, with the same name, in the base class. It also hides all base class methods with the same signature.
*.An indexer introduced in a class or struct hides all base class indexers with the same signature.
http://msdn.microsoft.com/en-us/library/435f1dw2.aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

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

Posted by: Bandi on: 9/20/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
The best example for when to use new keyword with function (i.e. new modifier):

You can use it to imitate return type covariance.
abstract class Enclosure

{
protected abstract Animal GetContents();
public Animal Contents() { return this.GetContents(); }
}
class Aquarium : Enclosure
{
public new Fish Contents() { ... }
protected override Animal GetContents() { return this.Contents(); }
}

This is a work-around. public Fish Contents() { ... } is not legal, despite being safe.

In general, you should not use method hiding, as it is confusing to consumers of your class (the specific example above does not suffer from this problem). Just name your new method something else if you don't want to override an existing method.

A likely real-world situation where you would need method hiding is if the provider of a base class added a generic method which you had already added to a derived class. Such a program will compile (and give warnings) without the new keyword, but adding new says, "I know my version of this method is replacing the base class's version. This is horrible and confusing, but we're stuck with it." That's still better than forcing the derived class to rename their method.

Just allowing the derived method to be treated as being an override would cause problems. Ignoring any concerns with implementing the compiler, the new method is semantically different from the base method, but polymorphism would cause the new method to be called when asked to call a method with the same name.

Note:
1) New methods are expected to hide base methods. The new modifier specifies that a method is supposed to hide a base method. It eliminates a warning issued by the compiler. No functionality is changed. But an annoying warning is silenced.
2) 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.
3) In some scenarios the new keyword prevents a warning from the C# compiler.



References:
http://blogs.msdn.com/b/ericlippert/archive/2004/01/07/virtual-methods-and-brittle-base-classes.aspx
http://shivasoft.in/blog/microsoft/csharp/virtual-override-and-new-keyword-in-c/


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

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

Posted by: Bandi on: 9/20/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
click on "mark it as answer"

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

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

Login to post response