C#是否有等效的Java静态嵌套类?
我将Java转换为C#,并具有以下代码(请参阅关于它的用法的讨论)。一种方法可能是创建一个单独的文件/类,但是有一个C#idom,它保留了Java代码的意图。
I am converting Java into C# and have the following code (see discussion in Java Context about its use). One approach might be to create a separate file/class but is there a C# idom which preserves the intention in the Java code?
public class Foo {
// Foo fields and functions
// ...
private static class SGroup {
private static Map<Integer, SGroup> idMap = new HashMap<Integer, SGroup>();
public SGroup(int id, String type) {
// ...
}
}
}
看看
http://blogs.msdn.com/oldnewthing/archive/2006/08/01/685248.aspx a>
我正在查找
换句话说,类是
语法糖,不可用
到C#。在C#中,您必须手动执行
。
In other words, Java inner classes are syntactic sugar that is not available to C#. In C#, you have to do it manually.
如果愿意,您可以创建自己的
糖:
If you want, you can create your own sugar:
class OuterClass {
...
InnerClass NewInnerClass() {
return new InnerClass(this);
}
void SomeFunction() {
InnerClass i = this.NewInnerClass();
i.GetOuterString();
}
}
你想用Java写
new o.InnerClass(...)你可以在
C#中写入o.NewInnerClass(...)或new
InnerClass(o,...)。是的,它只是一个
一堆的移动这个词新。
像我说的,只是糖。
Where you would want to write in Java new o.InnerClass(...) you can write in C# either o.NewInnerClass(...) or new InnerClass(o, ...). Yes, it's just a bunch of moving the word new around. Like I said, it's just sugar.