[英]C# Can namespaces only access methods which are declared with the static keyword?
I'm new to C#.我是 C# 的新手。 The question i had was regarding namespaces.
我的问题是关于命名空间。 This is what i have.
这就是我所拥有的。
namespace A {命名空间 A {
namespace B {
public class Family {
public static void Print() {
Console.WriteLine("HIIIIIIIII");
}
}
}
} }
Now I can access the method "Print" in the main using ---------------ABFamily.Print();-------------------现在我可以使用 ---------------ABFamily.Print();---------------- 在 main 中访问方法“Print” ---
But I cannot access the Print method if i remove the static keyword in the declaration.但是,如果删除声明中的 static 关键字,则无法访问 Print 方法。 Is it because you can only access static methods inside a namespace?
是因为您只能访问命名空间内的静态方法吗?
Thanks in advance提前致谢
Static methods can be called the way you showed.静态方法可以按照您展示的方式调用。 Non static methods (also called instance methods) need an object instance so that you call.
非静态方法(也称为实例方法)需要一个对象实例以便您调用。
So in order to call your methods after you remove static
keyword from your method you need to create an object instance.因此,为了在从方法中删除
static
关键字后调用您的方法,您需要创建一个对象实例。
For example:例如:
var myFamily = new A.B.Family();
myFamily.Print();
For more info refer to C# Documentation有关更多信息,请参阅C# 文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.