C#有Java静态嵌套类的等效? [英] Does C# have an equivalent of Java static nested class?

查看:111
本文介绍了C#有Java静态嵌套类的等效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我转换成Java的C#和有以下代码(见的 Java中的语境下讨论了解其使用)。一种做法是创建一个单独的文件/类,但有它保存在Java代码意图的C#IDOM?



 公共类Foo {

// Foo的领域和功能
// ...
私有静态类SGroup {
私有静态地图<整数,SGroup>的idMap =新的HashMap<整数,SGroup>();

公共SGroup(INT ID,字符串类型){
// ...
}
}
}


解决方案

给这个一看
http://blogs.msdn.com/oldnewthing/archive/2006/08/01/685248.aspx



我在



寻找具体

在换句话说,爪哇内部类是
语法糖不可用
到C#。在C#中,你必须这样做手工



如果你愿意,你可以创建自己的
糖:

 类OuterClass {
...
将InnerClass NewInnerClass(){
返回新将InnerClass(本);
}
无效SomeFunction(){
我将InnerClass = this.NewInnerClass();
i.GetOuterString();
}
}

您想在Java中$ B $写b新o.InnerClass(...),你可以在
C#或者o.NewInnerClass(...)或新的
写将InnerClass(0,...)。是的,它只是一个$ B移动新词左右的$ B一群。
就像我说的,这只是糖。



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) {
    // ...
            }
        }
    }

解决方案

Give this a look http://blogs.msdn.com/oldnewthing/archive/2006/08/01/685248.aspx

I am looking specifically at

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();
 }
}

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.

这篇关于C#有Java静态嵌套类的等效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆