开发者

ArrayList: help to translate from Java to C#

How can I translate this Java code:

private List<MPoint> classes = new ArrayList<MPoint>(); // MPoint is my own class

to C#? And what does final keyword mean in Java? What analogue there is to it in C#? I am newbie in C# and Java and I need h开发者_开发百科elp :)


private IList<MPoint> classes = new List<MPoint>();

IList<T> is an interface implemented by the generic collection class List<T>.

final has different meanings in Java depending on its context:

  • final on a class or method is like sealed in C#
  • final on a variable is like readonly in C#.

Equivalent of final in C# is discussed in detail here.


private IList<MPoint> classes = new List<MPoint>(); 

The Java List interface is analogous to the .NET IList<T> interface. The ArrayList concrete type is is analogous to the .NET List<T> type.

final is a bit more complicated. In this case (field declaration + initializer), the closest would be readonly. On the other hand, as of the current version, there's no way to mark a local variable as final in C#. (Off-topic, final in the context of compile-time constants = const. In the context of "cannot override / inherit further" = sealed).

EDIT: To answer your question about what "final" means, it is normally used to indicate a kind of immutability - that something cannot be further modified or overridden. What sort of mutability is actually prevented with that modifier and its C# analogues depends on context and is hard to describe without going into specifics.


It is similar to what you've written.

List<MPoint> classes = new List<MPoint>();


Final in Java means that after the first assignment to a final variable you can not change it anymore. I am unsure what is the equivalent in C#.

The code is the same as AS-CII said but I would have written it like

private IList<MPoint> classes = new List<MPoint>();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜