Hello friends I face this question in one Interview in statics(Myclass). I have two clases one is M1 and M2.
How can I call m2 in m1 and m1 in m2? and also How to create an Instance of an static?
static void Main(string[] args)
{
//in this Portion How can I call all member if MyClass
}
public static class Myclass
{
public static class M1
{
//Here How can I call m2
}
public class m2
{
//Here How can I call m1
}
}
For static class M1, you need to call using class name.
For non static class M2, you need to create instance.
you can call methods/properties, and not classes.
public static class Myclass
{
public static void Main(String[] args)
{
// call something on m2
var m2 = new Myclass.m2();
m2.A2(); // call m2.A2 method
// call something on m1
Myclass.m1.A1();
}
public static class M1
{
//Here How can I call m2
public static void A1()
{
var m2 = new Myclass.m2(); // create m2instance
m2.A2(); // call m2
}
}
public class m2
{
//Here How can I call m1
public void A2()
{
Myclass.M1.A1(); // called M1.A1
}
}
}
You can not call a class, and as it is static you can not Generate new class of that type but you can call methods and functions of it easily like this:
public static class Myclass
{
public static class M1
{
public static class Method2(){
}
new m2().AMethod();
}
public class m2
{
//Here How can I call m1
public static void AMethod(){
//method
}
}
}
And for main:
static void Main(string[] args)
{
new MyClass.m2().AMethod();
MyClass.M1.Method2();
}
Related
This question already has an answer here:
Inheritance and extension method
(1 answer)
Closed last year.
I have class A which is a base class for B. And an extension for both types. Then if I call an extension from a generic function (even with an object of type B) it still calls an extension for a base class
void Main()
{
var b = new B();
GenericClass.SomeGenericFunction<B>(b);
}
public class A
{
public void fooA()
{
Console.WriteLine("A");
}
};
public class B : A
{
public void fooB()
{
Console.WriteLine("B");
}
};
public static class Extension {
public static void SomeMethod(this A a)
{
a.fooA();
}
public static void SomeMethod(this B b)
{
b.fooB();
}
};
public static class GenericClass {
public static void SomeGenericFunction<T>(T someObject) where T : A, new()
{
someObject.SomeMethod();
}
};
The question basically is why the result is?
A
Generic method for reference types shares implementation for all reference types so compiler will resolve SomeMethod only one time during compilation - for "highest" class in hierarchy - A. You can workaround that either by changing A (and B) by making SomeMethod an virtual instance method of A hierarchy or using type checks in your generic function:
public static class GenericClass
{
public static void SomeGenericFunction<T>(T someObject) where T : A, new()
{
switch (someObject)
{
case B b:
b.SomeMethod();
break;
case A:
default:
someObject.SomeMethod();
break;
}
}
}
Or diving into reflection.
try this
public class A
{
public virtual void foo()
{
Console.WriteLine("A");
}
};
public class B : A
{
public override void foo()
{
Console.WriteLine("B");
}
};
public static class Extension
{
public static void SomeMethod(this A a)
{
a.foo();
}
public static void SomeMethod(this B b)
{
b.foo();
}
};
public static class GenericClass
{
public static void SomeGenericFunction<T>(T someObject) where T : A, new()
{
someObject.SomeMethod();
}
};
test
var b = new B();
GenericClass.SomeGenericFunction<B>(b);
result
B
if you need to keep your classes, you can use this code as well. It is easier to extend and mantain. I only don't understand why do you need extensions if you use GenericClass? Maybe it is easier to use as C bellow? Or just use extensions. It seems as they repeat each other.
public static class GenericClass
{
public static void SomeGenericFunction(B someObject)
{
someObject.SomeMethod();
}
public static void SomeGenericFunction(C someObject)
{
someObject.fooC();
}
public static void SomeGenericFunction<T>(T someObject) where T : A, new()
{
someObject.SomeMethod();
}
}
using System;
namespace Testing
{
class Program
{
static void Main(string[] args)
{ }
}
public class A
{
public void method1()
{ }
}
public class B : A
{
public void method2()
{ }
}
public class Test
{
A a = new A();
a.method1();
}
}
Please paste this code in VS and Please explain me why it is not in current context?
Inside a class is not the right place to call most functions:
public class Test
{
A a = new A();
a.method1();
}
Just put stuff into the main function, wich is there specifically for that part of the programming:
static void Main(string[] args)
{
A a = new A();
a.method1();
}
you can not have statements directly in class.
public class Test
{
A a = new A();
a.method1(); // this is not possible.
}
please modify your class as below:
public class Test
{
public void InvokeMethodOnA()
{
A a = new A();
a.method1();
}
}
or like this;
public class Test
{
A a = new A();
public void InvokeMethodOnA()
{
a.method1();
}
}
You can call method in body of method (sounds strange, but I don't have a better explanation).
In your case you mix a definition of the class Test with a context of function. I hope code with comments will be more descriptive:
public class Test
{
// this is not a local variable, this is a definition of field with initialization
A a = new A();
// you try call method on field, but in context of class definition, which is prohibited
a.method1();
}
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 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.
Having an event like this:
class ABC
{
delegate bool X (int a);
event X eventX;
}
ABC.eventX+=someMethod; //works
I assume the delegate is then created implicitly by compiler?
Yes, prior to .NET 2 you had to manually specify it:
ABC.eventX+=new X(someMethod);
But it is now created implicitly with this syntax:
ABC.eventX+=someMethod;
Yes, it's automatically created.
For example:
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
(new Program()).Entrance();
}
public void Entrance()
{
ABC a = new ABC();
a.eventX += callback;
}
protected bool callback(int a)
{
return true;
}
}
class ABC
{
public delegate bool X(int a);
public event X eventX;
}
}
The Program class will be this if you see in reflector:
internal class Program
{
// Methods
protected bool callback(int a)
{
return true;
}
public void Entrance()
{
ABC a = new ABC();
a.eventX += new ABC.X(this.callback);
}
private static void Main(string[] args)
{
new Program().Entrance();
}
}