• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Is creation of list simpler in C# as compared to java with absence of different kinds of list?

 
Ranch Hand
Posts: 1173
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



In Java to create a list we have to decide whether to use ArrayList or LinkedList and do as below example:





In C# we can create a list simply as below:




Is creation of list simpler in C# as compared to java?

thanks

 
Saloon Keeper
Posts: 10495
224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be careful that you don't forget how to write Java properly as you're learning C#. The String class' name starts with an uppercase letter, and variable names should start with a lowercase letter.

In C# you also have to decide what kind of list you want to use. You chose to use the C# version of an ArrayList, which is called List. The common list interface is called IList, not List. So essentially you wrote the C# equivalent of:

This is poor form.
 
Monica Shiralkar
Ranch Hand
Posts: 1173
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This is poor form.



Why is it poor form? Is it because it is not written as below?

List<String> authorList = new ArrayList<>();

 
Marshal
Posts: 65374
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:. . . Why is it poor form? Is it because it is not written as below? . . .

Stephan explained exactly what you are doing wrong. He showed you the different naming conventions, that C# interfaces have names starting I. You are therefore declaring your object by its implementation (List) rather than by its interface (IList). Don't let the difference between the Java┬« name (=ArrayList) and the C# name (=List) confuse you.
 
Stephan van Hulst
Saloon Keeper
Posts: 10495
224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:Why is it poor form? Is it because it is not written as below?


Yes, that would have been the correct way to do it in Java if you had declared the field as private. In C#, the correct way is:

Note the following differences:

  • C# uses IList instead of List.
  • C# uses List instead of ArrayList.
  • In C# you write built-in reference types (like object and string) with a lowercase letter.
  • C# doesn't support generic type inference for constructor calls; there's nothing similar to the diamond operator in Java.

  • Private fields should be written with a lowercase letter. I don't know what the convention is for internal, protected or public fields, but it doesn't matter: Fields should be private. The default visibility in C# for fields is private, but it's better to state it explicitly.
     
    Monica Shiralkar
    Ranch Hand
    Posts: 1173
    5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for summarising.
     
    Mo-om! You're embarassing me! Can you just read a tiny ad like a normal person?
    create, convert, edit or print DOC and DOCX in Java
    https://products.aspose.com/words/java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!