This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • 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
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

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

 
Ranch Hand
Posts: 1257
6
  • 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: 10655
227
  • 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: 1257
6
  • 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: 65772
250
  • 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: 10655
227
  • 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: 1257
    6
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for summarising.
     
    Destroy anything that stands in your way. Except this tiny ad:
    Java file APIs (DOC, XLS, PDF, and many more)
    https://products.aspose.com/total/java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!