This question already has answers here:
How to use class name as parameter in C#
(4 answers)
Closed 5 years ago.
Is it possible to pass ClassA as argument without using typeof or initializing it?
class ClassA {
}
class ClassB {
public void Function([something here] clazz) {
......
}
}
class ClassC {
public void main() {
ClassB asdf = new ClassB();
asdf.Function(ClassA); // pass like that, not typeof() or something else
}
}
Yes, you can pass the type as a generic parameter.
class ClassA {
}
class ClassB {
public void Function<T>() {
......
}
class ClassC {
public void main() {
ClassB asdf = new ClassB();
asdf.Function<ClassA>(); // magic
}
}
This sort of thing is very common with IoC containers and autofactories:
var o = container.Resolve<SomeClassName>();
Related
This question already has answers here:
Difference between new and override
(14 answers)
what is "public new virtual void Method()" mean?
(4 answers)
Closed 2 years ago.
In this example it uses polymorphysm via virtual, override keywords
abstract class A
{
public virtual string Print() { return "A"; }
}
class B : A
{
public override string Print() { return "B"; }
}
class C : B
{
public virtual new string Print() { return "C"; }
}
class D : C
{
public override string Print() { return "D"; }
}
class Program
{
static void Main(string[] args)
{
A a = new D();
Console.WriteLine(a.Print());
}
}
Console prints B. Why B, not D? Thank you for answers
Simple, because the last time you override the Print function of class A is during the declaration of class B
class B : A
{
public override string Print() { return "B"; }
}
By declaring public virtual new, you are hiding the underlying implementation of Print. So during the D declaration you are overriding the new implementation, declared at class C
Now A, has no knowledge whatsoever of what you've done, once you've hidden the function on B
That's the problem with methods that hide methods of their parent classes. It is not polymorphic. Since the variable is of type A the resolution of method Print() only knows about A.Print() and B.Print().
C.Print() and D.Print() are not overrides of A.Print(), because of the declation of C.Print() with new.
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.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
In C#, how can I get the inherited Class Type of an object where the object is an inherited object instead of the Type of the instantiated object?
Here is some code:
public class ClassA
{
public virtual void GetClassType()
{
var type = this.GetType();
Console.WriteLine(type.Name);
}
}
public class ClassB : ClassA
{
public override void GetClassType()
{
base.GetClassType();
var type = this.GetType();
Console.WriteLine(type.Name);
}
}
public class ClassC : ClassB
{
public override void GetClassType()
{
base.GetClassType();
var type = this.GetType();
Console.WriteLine(type.Name);
}
}
private void GetClassType()
{
var objectA = new ClassC();
objectA.GetClassType();
}
The GetClassType method outputs the following to the console:
ClassC
ClassC
ClassC
Is it possible to get the Type of the Class, instead of the Type of the object such that the following is the console output?
ClassA
ClassB
ClassC
If you have something like this
class A {}
class B : A {}
You can do this:
var b = new B();
var baseType = b.GetType().BaseType;
If you got the class which inherits the base type you already know which class it inherits.
if you want to check you can do as follows.
if(object is BaseType)
{
//Dostuff
}
using System;
class MyClass
{
}
class MyClass2: MyClass
{
}
namespace Appl
{
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine(typeof(MyClass2).BaseType.Name);
}
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Is it possible to create a variable that represent many class object and call a function that exist in all that class?
I want to create something like this..
class A{
void DoSomething(){
//some code
}
}
class B{
void DoSomething(){
//some code
}
}
class Controller{
public ClassType theObject;
public void DoSomething(){
theObject.DoSomething();
}
}
class Main{
public static void main(){
A objectA = new A();
B objectB = new B();
Controller objectC = new Controller();
objectC.theObject = objectA;
objectC.DoSomething();
objectC.theObject = objectB;
objectC.DoSomething();
}
}
Is anyone know how to do this (what keyword to change the ClassType in class Controller) or what keyword I need to use to search something like this?
You can create an Interface and let both classes A and B implement it.
interface ICanDoSomething {
void DoSomething();
}
class A : ICanDoSomething {
void DoSomething(){
//some code
}
}
class B : ICanDoSomething{
void DoSomething(){
//some code
}
}
class Controller {
public ICanDoSomething theObject;
public void DoSomething(){
theObject.DoSomething();
}
}
....
I've tried to keep the answer as similar to your code as possible. What you are looking are called interfaces. As a very basic explanation, classes that implement interfaces are required to provide code for each method that is defined in the interface. Hence by polymorphism, you are able to assign an instance of a concrete class such as A and B to a ClassType, which is your interface. You can then call any method that is defined in the interface on the object that is currently assigned.
interface ClassType
{
void DoSomething();
}
class A : ClassType
{
void DoSomething()
{
//some code
}
}
class B : ClassType
{
void DoSomething()
{
//some code
}
}
class Main
{
public static void main()
{
A objectA = new A();
B objectB = new B();
ClassType objectC;
objectC = objectA;
objectC.DoSomething();
objectC = objectB;
objectC.DoSomething();
}
}
This question already has answers here:
new keyword in method signature
(9 answers)
Closed 9 years ago.
I have seen methods that are declared like this:
public void new SortItems()
What does this actually do? I know that the new keyword is used to invoke constructors, but I have also seen it on method definitions like the example above.
When use this way, it's a modifier. It's used to hide an inherited member rather than to override it. This is useful if the base method is sealed. Here's a quick example to demonstrate the difference between overriding and hiding inherited members:
public class Foo
{
public virtual void OverriddenMethod() { Console.WriteLine("foo"); }
public void HiddenMethod() { Console.WriteLine("foo"); }
}
public class Bar : Foo
{
public override void OverriddenMethod() { Console.WriteLine("bar"); }
public new void HiddenMethod() { Console.WriteLine("bar"); }
}
void Main()
{
Bar bar = new Bar();
Foo foo = bar;
bar.OverriddenMethod(); // "bar"
bar.HiddenMethod(); // "bar"
foo.OverriddenMethod(); // "bar"
foo.HiddenMethod(); // "foo"
}
Further Reading
new Modifier (C# Reference)
It should be like this:
public new void SortItems(){
//...
}
This new keyword is used to shadow the base member (method, property, ...) which has the same name (for property, event...) and same signature (for method), in this case it is the method SortItems. It's different from the new in creating new instance. No matter using new to shadow the conflicted member in base class or not, to access the base member you have to use the keyword base to access it in the derived class.
When used in a method signature, it means that the implementation details are different for the class defining them. The problem with this approach is that it is not used polymorphically so:
class Thing
{
void DoSomething()
{
Console.WriteLine("Thing");
}
}
class Other : Thing
{
new void DoSomething()
{
Console.WriteLine("Other");
}
}
var thing = new Thing();
thing.DoSomething(); \\ prints Thing
var other = new Other();
other.DoSomething(); \\ prints Other
((Thing)other).DoSomething(); \\ prints Thing
It is the opposite of override. Say you have:
public class A
{
public virtual void f() { Console.WriteLine( "A" ); }
}
public class B : A
{
public override void f() { Console.WriteLine( "B" ); }
}
public class C : A
{
public new void f() { Console.WriteLine( "C" ); }
}
And then in main:
A b = new B();
A c = new C();
b.f();
c.f();
(c as C).f();
This would print:
B
A
C
It will only call the new method when the type is that of the defining class.