Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArrayList: help to translate from Java to C#

Tags:

java

c#

arraylist

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

like image 805
Siarhei Fedartsou Avatar asked Nov 20 '10 15:11

Siarhei Fedartsou


2 Answers

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.

like image 181
Steve Townsend Avatar answered Oct 13 '22 13:10

Steve Townsend


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.

like image 20
Ani Avatar answered Oct 13 '22 12:10

Ani