What is the default value of a primitive type variable in C #?

1

When a primitive type variable is created but not initialized, its value is null ?

Example:

public int x;

If I do nothing with x, ie do not assign a value to it, it will null ?

    
asked by anonymous 04.05.2018 / 22:45

1 answer

2

C # does not have types called primitive, these types are by value. The default value of all of them is 0, each with its representation of 0.

The null is a reference with value 0, so it does not have an object associated with it. Only types by reference can be effectively null, so one type per value can not be null.

Even there are types that simulate nullity, for example a int? may be null, but in practice it is only a composition that indicates nullity, and has undesirable side effects.

In the construction of the object this variable will be zeroed. In local variables it will not compile, it is mandatory to put a value, even if it is the default value, you can even use default instead of value, if it is the most appropriate semantics. Of course the type is not being inferred.

One type per reference will be null.

In C # 8 the use of nulls will be discouraged. It will serve much better than it already is as a valid alternative to exceptions in flow control (which is the wrong mechanism).

    
05.05.2018 / 16:11