1

I know that in C# we cant override non virtual fields and methods but I have the following case:

Class Base { public static int a {get;set;} public static void b() { // it uses a } public static void c() { // it uses a } public static string d {get {return a.ToString();}} } Class MyClass :Base { //... } 

now in my class MyClass i want to override the property a that all of Base class methods and properties start using the the overwritten property that i implemented, taking into considerations that i don't have access to change Base class

Is there any way to do this even if i had to re-implement the getter method of that property?

4
  • 7
    why do you have all static methods!? Commented Nov 18, 2013 at 16:02
  • @DanielA.White it from a generated file from the .net framework Commented Nov 18, 2013 at 19:07
  • the .net framework does not generate classes (unless you tell it to) please elavorate. Commented Nov 18, 2013 at 19:07
  • @DanielA.White i was talking about resource files and "PublicResXFileCodeGenerator" Commented Nov 19, 2013 at 5:45

2 Answers 2

3

This is a static property, so it's used in the form Base.a. Whatever you do, the calling code will still resolve to the Base class, and then to it's static property a.

E.g

//old calling code Base.a = 7; // does not create an instance Console.WriteLine(Base.a); class MyClass :Base { public static string a {get; set;} } //new calling code MyClass.a = "some string"; // uses whatever a you defined in MyClass Console.WriteLine(MyClass.a); 
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, you are correct, i was just a little bit confused. thank you
2

The best way is perhaps to introduce a new property by a different name, or have your class wrap the base class instead of extending it. Together with extension methods, these are the most common ways to build upon functionality of a class you don't have access to.

You can also re-introduce the property using the new keyword, but I would not recommend that. It hides the base class methods, which is dangerous because users of your code may end up calling the wrong thing. Whether new is warranted in this case very much depends on your exact use case.

Finally note, as Daniel remarked in the comments to your original post, that the methods in your base class seem to be static. So even if you had base class access, you would not be able to make them virtual. Actually having loads of static methods is generally bad design, except special cases such as object factories or extension method containers.

4 Comments

That doesn't introduce override semantics, as the question requests. There's also no need to use new to have a property of the same name in an inheriting class; with the properties being static there isn't anything to actually shadow.
Correct, the only way to get proper override is by making the base method virtual. Hiding the base class property and composition are the only alternatives I know of. They serve different purposes however, and which one is chosen depends on why OP wants to do what they want.
The property is static, so it couldn't even be virtual if he wanted it to be virtual.
Yep, that is the other issue with the code, as remarked by Daniel in the comments already.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.