This question already has answers here:
Can a static method be overridden in C#?
(3 answers)
Closed 7 years ago.
I have a base class with a protected-level static variable, a protected-level static function, and a public function:
public class BaseClass
{
protected static int Size = 4;
public static byte[] DoSomething(byte[] params)
{
// use Size somehow
params = DoSomethingElse(params);
return params;
}
protected static byte[] DoSomethingElse(byte[] params)
{
// do whatever
return params;
}
}
And a derived class that hides/overrides the protected-level variable and function:
public class DerivedClass : BaseClass
{
public new static int Size = 2;
protected new static byte[] DoSomethingElse(byte[] params)
{
// do something different than base class
return params;
}
}
Now, when I call DerivedClass.DoSomething, I want the Size value and DoSomethingElse from the DerivedClass to be used, but the BaseClass values are used instead. Is there a way to make this use the DerivedClass variable and method?
You can't override static members. They belong to the class itself and are not associated with any instance of a class, so the concept is not applicable.
Related
Recently, I had a need to process the private data contained in the base class using the methods of the child class. My base class could only contain domain-specific types (it only represents data). So first I decided to create a child-class in another project and implement the processing logic in it. But the problem is that once you create an instance of the base class, you can't cast it to the child type:
public class A
{
protected int member1;
public A(int value)
{
member1 = value;
}
}
public class B : A
{
public B (int value) : base(value)
{ }
public void DoSomething()
{
Console.Write(member1 * member1);
}
}
class Program
{
static void Main(string[] args)
{
A obj1 = new A(5);
B obj2 = (B)obj1; // InvalidCastException
obj2.DoSomething();
}
}
And I started thinking towards extension methods. However, you can't just access the protected fields of the class from them. In the end, I tried to combine the two approaches.
Here's my solution:
Make sure that you are allowed to add new methods to your base class and that your class is not sealed.
Add protected static method which returns the protected member you need.
Create an Extension class for your base class.
In extension class create a private nested class.
Inherit your nested class from your base class.
Create static method in nested class and implement the processing logic in (you can call static protected method from base class to get protected member from base class).
Create extension method in extension class and call static method of nested class in it.
The sample code is shown below:
public class A
{
protected int member1 = 0;
public A() {}
public A(int value)
{
member1 = value;
}
protected static int GetProtectedMember(A objA)
{
return objA.member1;
}
}
public static class AExtensions
{
public static void DoSomething(this A objA)
{
B.DoSomething(objA);
}
private class B : A
{
public static void DoSomething(A objA)
{
// objA.member1 // it's not allowed
int protectedFromA = A.GetProtectedMember(objA);
int sqr = protectedFromA * protectedFromA;
Console.WriteLine(sqr);
}
}
}
class Program
{
static void Main(string[] args)
{
A obj1 = new A(5);
obj1.DoSomething(); // 25
}
}
This way you can keep the classes that represent the data in a separate project and have multiple implementations of processing this data in different projects.
This question already has answers here:
Using static method from generic class
(4 answers)
Closed 2 years ago.
So I have, let's say, some class that requires generic type as a parameter:
public class SomeClass<GType> { }
And there I have a static method that doesn't interact with this generic type in any way:
public static void Method() { }
The problem is, when I try to call this method, it requires me to specify this generic type. I could, in fact, put there any type and that would work, but it just doesn't seem quite right, does it?
public class SomeClass
{
public static void DoSomething()
{
}
}
public class SomeClass<GType> : SomeClass
{
public void DoAnotherThing<GType>()
{
}
}
public static void Main(string[] args)
{
SomeClass.DoSomething();
}
This question already has answers here:
How do I call a derived class method from the base class?
(2 answers)
Closed 4 years ago.
I have this array in program
ClassA[] array=new ClassA[20];
array[0]=new ClassB();
array[1]=new ClassA();
This is the class file
public class ClassA
{
public void method()
{
Console.WriteLine("1");
}
}
public class ClassB : ClassA
{
public void method()
{
Console.WriteLine("2");
}
}
It writes 1, in both cases, but i want in first case to write 2, (to call method of ClassB).
How to do that?
You have to use override keyword in C# when you want to override method in child class.
public class ClassA
{
public virtual void method()
{
Console.WriteLine("1");
}
}
public class ClassB : ClassA
{
public override void method()
{
Console.WriteLine("2");
}
}
Regards.
This question already has answers here:
Overload resolution and virtual methods
(5 answers)
Closed 5 years ago.
Case 1:
public class BaseClass
{
public virtual void Print(int i)
{
Console.WriteLine("BaseClass Print(int)");
}
}
public class DerivedClass : BaseClass
{
public override void Print(int i)
{
Console.WriteLine("DerivedClass Print(int)");
}
public void Print(object obj)
{
Console.WriteLine("DerivedClass Print(object)");
}
}
static void Main(string[] args)
{
DerivedClass objDerivedClass = new DerivedClass();
int i = 10;
objDerivedClass.Print(i);
}
Output is DerivedClass Print(object).
Case 2:
public class SomeClass
{
public void Print(int i)
{
Console.WriteLine("DerivedClass Print(int)");
}
public void Print(object obj)
{
Console.WriteLine("DerivedClass Print(object)");
}
}
static void Main(string[] args)
{
SomeClass objSomeClass = new SomeClass();
int i = 10;
objSomeClass.Print(i);
}
Output is DerivedClass Print(int).
After calling objDerivedClass.Print(i); method, the output is DerivedClass Print(object). I don't understand why the method Print(object obj) is being called instead of Print(int i).
If DerivedClass does not inherit BaseClass class then output is DerivedClass Print(int).
Please explain.......
This is how overload resolution works with inheritance:
The function that includes the override modifier is excluded from the set of candidates.
Since the function with the object parameter can be used, then the function that is declared in the base is removed from the set of candidates.
The winner is the function with the object parameter.
According to Eric Lippert (from Microsoft):
This is by design and for a good reason. This design helps prevent the Brittle Base Class problem. C# was designed to make it easier and safer to write "versioned" components, and this rule is a big part of that.
When no inheritance is used, both functions are candidates, and the one that is more specific is used. The winner is the function with the int parameter.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
.NET: Determine the type of “this” class in its static method
How can I make GetType() accessible from a static method?
I have this abstract base class
abstract class MyBase
{
public static void MyMethod()
{
var myActualType = GetType(); // this is an instance method
doSomethingWith(myActualType);
}
}
and an implementation of that class. (I could have many implementations.)
class MyImplementation : MyBase
{
// stuff
}
How can I get myActualType to be typeof(MyImplementation)?
The "type" within a static method is always the specific type, since there is no such thing as a virtual static method.
In your case, this means you can just write:
var myActualType = typeof(MyBase);
Since the "type" of MyMethod, being a static, is always a static method of MyBase.
What about this?
abstract class MyBase<T>
{
public static void MyMethod()
{
var myActualType = typeof(T);
doSomethingWith(myActualType);
}
}
class MyImplementation : MyBase<MyImplementation>
{
// stuff
}
This is the pattern i used.
abstract class MyBase
{
public static void MyMethod(Type type)
{
doSomethingWith(type);
}
}