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();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.