locked
Confused about this Collection RRS feed

  • Question

  • User-1865949272 posted

    hi guys

    Pardon me, but I have been away from c# for a while so confused about a few issues. 

    Why is this code declared as this 

       private static List<int> myCollection = new List<int>();

    why not like this

    private static  myCollection = new List<int>();

    please explain

    thanks

    E

    Friday, December 6, 2019 11:01 AM

Answers

All replies

  • User303363814 posted

    The object name is myCollection.

    It has an access of private

    It is a static object (shared by all instances of the class)

    The piece to the right of the = is how the object is initialised.

    The compiler needs to know the type of this object, that is given immediately before the name (myCollection).  Your second example does not tell the compiler the type of your object.

    It feels redundant when written as you have it but sometimes you might declare like this

    private static IEnumerable<int> myCollection = new List<int>();
    // or
    private static IList<int> myCollection = new List<int>();
    

    To tell the compiler that myCollection can be set to any specific type that implements the interface mentioned.  (Initially, it just happens to be List<int> but that could change later in the program with a new assignment to myCollection).

    If you don't care too much then there are circumstances where you can have

    private static var myCollection = new List<int>();

    and now the compiler will deduce that the type of the object is the same as the type that it is initialised to. (List<int>).  This has the advantage of reducing typing, not repeating yourself and allowing you to change the type later with a minimum of typing.  For example, you could change the initialisation to 

    new List<long>()

    without having to do it in two places.

    Friday, December 6, 2019 10:36 PM
  • User288213138 posted

    Hi surname,

    Why is this code declared as this 

       private static List<int> myCollection = new List<int>();

    why not like this

    private static  myCollection = new List<int>();

    You must declare the type of the object when you instantiate it, otherwise, an error occurs: The name 'myCollection' does not exist in the current context.

    This is the most basic knowledge of C #. you can refer to the below link about C #:

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/classes

    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/new-operator

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 9, 2019 3:32 AM
  • User-1340885213 posted

    You have to do that because C# is statically/strongly typed programming language. 

    There are several good reasons that you should declare the primitive or non-primitive data type while naming a field. It has many advantages over the dynamic language, which is duck typed.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 9, 2019 4:41 AM
  • User-1865949272 posted

    thank you

    Monday, December 9, 2019 2:55 PM