• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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

 
Ranch Hand
Posts: 2949
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15729
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2949
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79967
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15729
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2949
    13
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks for summarising.
     
    Did Steve tell you that? Fuh - Steve. Just look at this tiny ad:
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic