Why is the output B5, can someone explain?
class A
{
public virtual void M(int x=5)
Console.Write("A"+x);
}
class B:A
{
public override void M(int x=6)
Console.Write("B"+x);
}
class Program
{
static void Main(string[] args)
{
A a = new B();
a.M();
}
}
//Output is : B5
I expect the output to be B6, but the actual output is B5.
Default parameters don't work as you expect them.
They are constants that are hard-wired into method call during compilation. And during compilation, only known type is that of A.
Your code is equivalent to:
static void Main(string[] args)
{
A a = new B();
a.M(5);
}
Related
How can be call the method from another class inherited
let's see my code please:
class Program : classA
{
static void Main(string[] args)
{
// how i can call method ToDo without create an instance like below
//classA c = new classA();
//c.ToDo();
Console.ReadLine();
}
}
class Program2 : classB
{
static void Main(string[] args)
{
// how i can call method ToDo
//ToDo()
Console.ReadLine();
}
}
public abstract class classB
{
public void ToDo()
{
Console.WriteLine("classB");
}
}
public class classA
{
public void ToDo()
{
Console.WriteLine("classA");
}
}
how i can call the method in Either way, please help me.
There are a couple ways to do what you want to do (they're kind of similar or even the same).
One way is to create a class with a static method:
public class classA
{
public static void ToDo()
{
Console.WriteLine("classA");
}
}
then call it like:
classA.ToDo();
Another way is to add another static method to the class that contains Main:
class Program2 : classB
{
static void Main(string[] args)
{
ToDo()
Console.ReadLine();
}
static void Todo()
{
// do stuff here
}
}
If u want to call ToDo() function into [class Program : classA] and [class Program : classB]
Without creating Instance.
U have to Define ToDo() function as static, then u can call this method with class name in anywhere.
public static void ToDo(){}
I have two overloaded methods MyMethod. One with an int as parameter the other with a long.
Calling this method from outside with a integer calls the MyMethod(int) member. But if the method with int is overridden then the MyMethod(long) is called. I do not understand why dot.net behaves like this. Can someone explain the technical background?
For me it looks like a bug.
Example 1. and 3. (see code below) behaves like I would expect. But the 2. one I cannot explain why the code calls the long method.
// 1. Normal Overloading
class Program
{
static void Main(string[] args)
{
int value = 123;
new MyClass().MyMethod(value);
// Output: Method INT
Console.ReadKey();
}
}
class MyClass
{
public void MyMethod(int value)
{
Console.WriteLine("Method INT");
}
public void MyMethod(long value)
{
Console.WriteLine("Method LONG");
}
}
// 2. Combine Overriding with Overloading
class Program
{
static void Main(string[] args)
{
int value = 123;
new MyChildClass1().MyMethod(value);
// Output: Method LONG
Console.ReadKey();
}
}
class MyParentClass
{
public virtual void MyMethod(int value)
{
Console.WriteLine("Method INT");
}
}
class MyChildClass1 : MyParentClass
{
public override void MyMethod(int value)
{
Console.WriteLine("Method INT");
}
public void MyMethod(long value)
{
Console.WriteLine("Method LONG");
}
}
// 3. Inherit but use New for overloading
class Program
{
static void Main(string[] args)
{
int value = 123;
new MyChildClass2().MyMethod(value);
// Output: Method INT
Console.ReadKey();
}
}
class MyParentClass
{
public virtual void MyMethod(int value)
{
Console.WriteLine("Method INT");
}
}
class MyChildClass2 : MyParentClass
{
public new void MyMethod(int value)
{
Console.WriteLine("Method INT");
}
public void MyMethod(long value)
{
Console.WriteLine("Method LONG");
}
}
It's not a bug, it's a planned behavior (but I agree this is a bit of confusing especially in comparison with others examples). From here:
There's one aspect of this behaviour which is particularly surprising though. What counts as a method being "declared" in a class? It turns out that if you override a base class method in a child class, that doesn't count as declaring it.
Here is the code I currently have, the question follows after:
class Program
{
static void Main(string[] args)
{
var obj1 = new A();
obj1.DoIt();
obj1.SetFlyBehavior(new BehaviorB());
obj1.DoIt();
string input = Console.ReadLine();
}
};
class BaseOfA
{
protected ObjectBehavior behavior;
public void DoIt()
{
behavior.DoIt();
}
public void SetBehavior(ObjectBehavior ob) {
behavior = ob;
}
};
class A : BaseOfA {
public A() {
behavior = new BehaviorA();
}
}
interface ObjectBehavior {
void DoIt();
}
class BehaviorA : ObjectBehavior {
void ObjectBehavior.DoIt() {
Console.WriteLine("Behavior: A");
}
}
class BehaviorB : ObjectBehavior {
void ObjectBehavior.DoIt() {
Console.WriteLine("Behavior: B");
}
}
Now my question is, in this case, how am I going to make it work so that I can assign both BehaviorA and BehaviorB to instance obj1 as long as they implement ObjectBehavior?
You are calling obj.SetFlyBehaviour this method is not defined anywhere. The method you define on BaseOfA is called SetBehaviour. Once that is fixed the code you gave compiles fine for me
I am new to C# and I'm having a little problem with calling a function from the Main() method.
class Program
{
static void Main(string[] args)
{
test();
}
public void test()
{
MethodInfo mi = this.GetType().GetMethod("test2");
mi.Invoke(this, null);
}
public void test2()
{
Console.WriteLine("Test2");
}
}
I get a compiler error in test();:
An object reference is required for the non-static field.
I don't quite understand these modifiers yet so what am I doing wrong?
What I really want to do is have the test() code inside Main() but it gives me an error when I do that.
If you still want to have test() as an instance method:
class Program
{
static void Main(string[] args)
{
Program p = new Program();
p.test();
}
void Test()
{
// I'm NOT static
// I belong to an instance of the 'Program' class
// You must have an instance to call me
}
}
or rather make it static:
class Program
{
static void Main(string[] args)
{
Test();
}
static void Test()
{
// I'm static
// You can call me from another static method
}
}
To get the info of a static method:
typeof(Program).GetMethod("Test", BindingFlags.Static);
Just put all logic to another class
class Class1
{
public void test()
{
MethodInfo mi = this.GetType().GetMethod("test2");
mi.Invoke(this, null);
}
public void test2()
{
Console.Out.WriteLine("Test2");
}
}
and
static void Main(string[] args)
{
var class1 = new Class1();
class1.test();
}
The method must be static in order to call it.
Is it possible for the a.doStuff() method to print "B did stuff" without editing the A class? If so, how would I do that?
class Program
{
static void Main(string[] args)
{
A a = new A();
B b = new B();
a.doStuff();
b.doStuff();
Console.ReadLine();
}
}
class A
{
public void doStuff()
{
Console.WriteLine("A did stuff");
}
}
class B : A
{
public void doStuff()
{
Console.WriteLine("B did stuff");
}
}
I'm modding a steam game, Terraria. And I don't want to decompile and recompile it all because that will screw with steam. My program 'injects' into Terraria via XNA. I can use the update() and draw() methods from XNA to mod some things. But it's pretty limited. I wan't to override base methods to mod more things (worldgen for example).
Yes, if you declare doStuff as virtual in A and then override in B.
class A
{
public virtual void doStuff()
{
Console.WriteLine("A did stuff");
}
}
class B : A
{
public override void doStuff()
{
Console.WriteLine("B did stuff");
}
}
Since B is effectively A through inheritance and the method is overloaded.
A a = new B();
a.doStuff();
The code for class A & B you have posted will anyways generate below compiler warning and will ask to use the new keyword on class B, although it will compile:
The keyword new is required on 'B.doStuff()' because it hides inherited member 'A.doStuff()'
Use method hiding along with new and virtual keyword in class Mapper and class B as follows:
class Program
{
static void Main(string[] args)
{
Mapper a = new B(); //notice this line
B b = new B();
a.doStuff();
b.doStuff();
Console.ReadLine();
}
}
class A
{
public void doStuff()
{
Console.WriteLine("A did stuff");
}
}
class Mapper : A
{
new public virtual void doStuff() //notice the new and virtual keywords here which will all to hide or override the base class implementation
{
Console.WriteLine("Mapper did stuff");
}
}
class B : Mapper
{
public override void doStuff()
{
Console.WriteLine("B did stuff");
}
}