6

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 help :)

Siarhei Fedartsou
  • 1,843
  • 6
  • 30
  • 44

4 Answers4

7
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.

Community
  • 1
  • 1
Steve Townsend
  • 53,498
  • 9
  • 91
  • 140
4
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.

Ani
  • 111,048
  • 26
  • 262
  • 307
1

It is similar to what you've written.

List<MPoint> classes = new List<MPoint>();
as-cii
  • 12,819
  • 4
  • 41
  • 43
0

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>();
Tiago Veloso
  • 8,513
  • 17
  • 64
  • 85