C#语法[0] [英] C# grammar[0]
本文介绍了C#语法[0]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
namespace sa { public class Text { public class B { } public delegate void f(int a,int b); public void t(int x, int y) { Console.WriteLine((x+y)); } public static void Main() { B b=new B(); Text text=new Text(); text.t(2,3); } } }
t()
应该由文本对象使用.
为什么嵌套类B
可以直接使用?
编译器是否将类B
视为static
?
解决方案
您的主要方法是在Text类内定义的.因此,B类对它是直接可见的.
尝试将Main()移到另一个类中,它将对它不再直接可见.
shilf写道:B类是编译器静态的吗?
B类不是静态的.您将无法做到
B b=new B();
如果是这种情况.
对于嵌套类的不同示例,请此处.
>
namespace sa { public class Text { public class B { } public delegate void f(int a,int b); public void t(int x, int y) { Console.WriteLine((x+y)); } public static void Main() { B b=new B(); Text text=new Text(); text.t(2,3); } } }
t()
should be used by the text object.
Why can the nested class B
be used directly?
Is class B
considered static
by the compiler?
解决方案
Your main method is defined inside the class Text. So the B class is directly visible to it.
Try to move Main() into another class and it will be no more visible to it directly.
shilf wrote:is class B static by compiler?
Class B is not static. You would not be able to do
B b=new B();
if this was the case.
For different samples on nested classes, have a look here.
这篇关于C#语法[0]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文