Explanation in constructor method

Asked

Viewed 154 times

1

I have some questions about the method of constructing a class that I am seeing as an example, if anyone can explain thank you.

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 use of the word static in that case?

2nd doubt - why was declared categories as IList of Categoria and received a List, that is, what is the difference between List for IList?

  • 2

    If you add more snippets, you can give more context and get a better answer. But it seems that these two questions already have answers here. http://answall.com/q/86484/101 and http://answall.com/q/73633/101 (is Java, but works equally), http://answall.com/q/76316/101 and http://pt.stackoverflowcom/q/136913/101. Complement: http://pt.overfstacklow.com/q/139045/101. To static: http://answall.com/q/99603/101. http://answall.com/q/96710/101 (java, but equal). Example of use: http://answall.com/q/134576/101

  • I wanted to know why in this example was used Ilist to declare categories, could not have used only the direct List ? in the case: private Static List<Category> categories = new List<Category>

  • http://stackoverflow.com/questions/400135/listt-or-ilistt

  • No one thought I should close, I gave an answer that might be a little more useful.

2 answers

4


static

The static indicates that that member is part of the class and not object to be created for that class - the instance - and will exist only in every application, unlike the instances of objects of the class that 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 can help understand: How to store data in RAM and make it available to any module or class in my application?. In C# o static is the same, but some practices there are different.

X class interface

I imagine you know that the IList is a interface and the List is a class concrete. To say that there is a IList is a more general way of using the List that implements the IList. is interesting because it does not require the use of List, one day can change to another more suitable 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 it can do.

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

It may also be useful to read Arraylist x List.

0

good afternoon, let’s answer 1. The method has been declared static in order to be used within the class without the need to instantiate the class, as it is a method that always returns the same result and does not hold (stateless) has no need to be instilled. 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 case of List)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.