views:

167

answers:

4
abstract class SettingSaver
    {


        public abstract void add(string Name, string Value);
        public abstract void remove(string SettingName);


    }


class XMLSettings : SettingSaver
    {

        public override void add(string Name, string Value)
        {
            throw new NotImplementedException();
        }

        public override void remove(string SettingName)
        {
            throw new NotImplementedException();
        }




    }

Is there anyway that I can change the name of the add function in XMLSettings class to addSetting but make sure it overrides the add function in the SettingSaver? I know it should be definitely overridden in derived classes but just want to know whether I can use a different name :) Thanx in advance :D

+8  A: 

No, you can't change the name when you override a method in C#.

Of course, you can override the method and implement it just by calling a different method.

(On a naming convention note, methods are typically PascalCased, starting with a capital letter. Parameters are typically camelCased, starting with a lower case letter.)

Jon Skeet
Although the names of the arguments can be changed in the overridden function right?
Ranhiru Cooray
@Ranhiru: Yes. However, since VB (always) and C# (as of v4) support calls with named arguments, it is potentially very confusing to do so. I recommend against it.
Eric Lippert
+3  A: 

No you can't.

You can add another method that houses the same functionality, but you can't hide or rename the inherited method.

For an example of this the Stream class in the framework has a method called Close(). This is exactly the same as calling Dispose(), but given a name that is more in line with the concept of "opening" a steam to start using it. They haven't removed the Dispose() method, only given the client another (better named) way to access the same functionality.

Martin Harris
+3  A: 

No - that would somewhat violate Polymorphism (anything the parent can do, the children should be able to do).

You could create a new method and use metadata on the old method to direct anybody developing against the class to use the new class.

David Neale
+1  A: 

The method name and arguments is what identifies a method. If you change the method name, then you are identifying a different method. Since overriding is about defining the same method in a subclass, then you must use the same method name.

mdma