Why can not I declare an attribute using the keyword var?

14

The var keyword allows me to declare typed variables, and allows the variables to be defined implicitly.

Example: var i = 10;

The compiler will assume that my variable i is integer type int .

So, why does not the compiler allow me to use the var keyword for attribute declarations within a class?

See this example for illustration:

class ClasseExemplo 
{
    public var atributo; //Este atributo poderia ser declarado de forma implícita.

    public ClasseExemplo() 
    {
        this.atributo = null;
    }
}

The above example returns the following error:

  

Error 1 The type or namespace name 'var' could not be found   missing a using directive or an assembly reference?)

If this way I can not declare an attribute that can be of any type , could there be another way to do this?

    
asked by anonymous 11.02.2016 / 00:34

3 answers

13

The simple answer: because the language specification says you should not infer in this case. That is, the creators of language thought that this was not worth doing. There are languages that infer this.

Detailing

To achieve this the compiler would be much more complicated. Some situations are very difficult or even impossible to infer. Of course you could infer to some extent and demand the type in other cases, but chose not to invest in it (there are proposals to do so), even because some members of the class are part of the contract of it, in that case it is better to be more explicit, still that this does not convince me much. The problem is the huge work to do right.

In fact, some situations could make the compilation slow and confusing. There are cases of linked and even cyclical references. There are cases to compile that first need to compile other drives to ensure that it is with the most updated code, there are cases of static initialization where the order can not be guaranteed.

The more official answer can be obtained with one of the compiler creators .

It may have become more complicated with the ability to initialize the property too, in C # 6.

Using var does not mean that you can use any type , it just means you do not have to type the type.

In C # what is being called the attribute is actually a field . Attribute is something else .

    
11.02.2016 / 01:04
5

See, as already said in @bigown's response

  

Using var does not mean that you can use any type, it just means you do not have to type the type.

In simplest terms this means that it is the compiler that chooses the type of the variable for you. Then you may ask, "But how does he do that?"

It does this by looking at what's on the right side of the statement. Example:

var i = 10;

10 is integer, so the variable i will be of type int . If in the sequence I try to do

i = "qualquer coisa";

I'm getting the error

  

Can not implicitly convert type 'string' to 'int'

  

If this way I can not declare an attribute that can be of any type, could there be another way to do this?

Yes , you can use dynamic for this . Dynamic gives you the freedom to do what you want - a property that can have its type changed at any time. Here's an example:

public static void Main()
{
    dynamic i = 10;

    WriteLine(i.GetType()); //output: System.Int32

    i = "teste";

    WriteLine(i.GetType()); //output: System.String
}

Keep in mind that this can (and will) be a problem if you are not sure what you are doing. Maybe, in more detail, we can help you better with this, but only with what I said is difficult.

    
11.02.2016 / 11:20
4

The first problem is that implicit type variables have to be immediately initialized (and not worth = null ).

It's like @bigown said Usar var não significa que pode usar qualquer tipo, significa apenas que não precisa digitar o tipo.

If in the scope of a method you have var atributo = 5; atributo = "string"; you will immediately get a compilation error instead of execution !

But even if you did

class ClasseExemplo 
{
    public var atributo = 5;

}

Unable to declare class variables with implicit type.

It works like this:

First it maps all classes, class attributes, method declarations, and then analyzes the scope of the methods and analyzes the scope of the methods based on what it already knows about the first step that the type inference for the var. That is, for this to work they would have to rewrite the compilation method. (which they could implement, but chose not to).

An opinion on the issue addressed in the article / a> that in one of the topics talks about the difficulty of the compiler treating vars referencing other implicit types in a string is that they could specify that a var can not reference another type implicit in class attributes.

The limitation is probably because of a lack of interest.

    
11.02.2016 / 01:20