Explanation in constructor method

1

I have some doubts in the constructor method of a class that I'm seeing as an example, if anyone can explain thanks.

private static IList<Categoria> categorias = new List<Categoria>()
        {
            new Categoria() {
            CategoriaId = 1,
            Nome = "Notebooks"},

            new Categoria() {
            CategoriaId = 2,
            Nome = "Monitores"},

            new Categoria() {
            CategoriaId = 3,
            Nome = "Impressoras"},

            new Categoria() {
            CategoriaId = 4,
            Nome = "Mouses"},

            new Categoria() {
            CategoriaId = 5,
            Nome = "Desktops"},
        };

1st doubt - what is the word static in this case?

2nd question - why was it declared categories as IList of Categoria and received List , ie what is the difference of List to IList ?

    
asked by anonymous 28.02.2017 / 14:41

2 answers

4

static

static indicates that member is part of object class to be created for that class - the instance - and there will only be one in every application, unlike instances of class objects that member will be in each created object.

A more complete explanation can be seen in What is the sense of an attribute being private and static at the same time in a class ? . You can also see better the difference between the static member and the instance member (it's Java, but in C # it works the same): Difference between " Attribute "and" Instance Variable ". A real example might help you understand: How to store data in RAM and make it available to any module or class in my application ? . In C # static is the same, but some practices there are different.

Class X interface

I imagine you know that IList is a interface and List is a class concrete. Saying that there is a IList is a more general way of using List that implements IList . It is interesting because it does not require the use of List , one day you can switch to another more appropriate class that implements IList . In addition, all access to the object there can only be done in the methods listed in IList which is a certain protection of what you can do.

This occurs following a very common practice for generalizing algorithms and data structures without worrying about the detail of the implementation: Programming interface, and not for implementation, why? . A specific explanation about it can be read in Difference between ICollection, IList and List? .

It may also be useful to read ArrayList x List .

    
01.03.2017 / 13:10
0
Good afternoon, let's go to the answer 1. The method was declared static to be able to be used within the class without the need to instantiate the class, since it is a method that always returns the same result and does not have stateless, it does not need to be instantiated. 2. Ilist is an interface, which List implements it, so you can say that your method has a super type return (ILIst) and returns any type that implements it (in the List case)

    
28.02.2017 / 21:27