In Visual Studio 2012 i have this simple example here:
and when i debug the code and move my cursor over i to get it's current value, i expect something like
Use of unassigned local variable
but there is a 0 which was not set - why is there a 0?
When you declare any local/block variable, they didn’t get the default values. They must assigned some value before accessing it other wise compiler will throw an error. If the variable has a global scope then default value can be assigned and accessed. if the variable is of reference type then the default value will be null. this link contains default values for the primitive datatype:
The compiler will not permit this(since it is local/block variable):
public static void samplemethod()
{
int a;
int b = a;
}
where as the following code works fine since the variable have global scope:
public int i;
public void samplemethod()
{
int a;
int b = i;
}
That's because int is a Value Type not a Reference Type.
MSDN :
Variables that are based on value types directly contain values.
Assigning one value type variable to another copies the contained
value. This differs from the assignment of reference type variables,
which copies a reference to the object but not the object itself.
Have a look at Value Types and Reference Types.
I hope it turns out to be helpful.
Related
Some questions about null reference checking by the CLR in C#
Consider this:
class CS
{
public int X;
}
void Foo(CS c)
{
c.X += 10;
}
CS c = default(CS);
Foo(c);
obviously a NullReferenceException is thrown here.
Now CS becomes a struct
struct CS { public int X; }
CS c = default(CS);
Foo(c);
Now no exception is thrown
Then Foo is changed into taking a ref CS cs argument.
void Foo(ref CS c) { c.X += 10; }
Again no exception since CS is still a struct.
Now the questions.
Does the CLR generate an "if (x == null)" statement for every field reference of class objects or how does it do that?
Does the CLR do the same for ref arguments when the argument is a struct?
Is it possible to get null for the CS argument when CS is a struct?
Structs are value types, so they aren't null. (You'd have to make it a Nullable<CS> since value types can't be null - see Nullable docs)
default(CS) is a struct where X (another value type) is 0 (or more precisely, default(int)). All Fields in a struct are initialized to default(FieldType).
For more information about structs, check out the documentation.
Answers to your questions:
The act of dereferencing a reference to get to a member will throw the exception. No explicit or * implicit* if-statement is added but the effect is the same.
No, it does not. A struct variable can never be null. The compiler knows this.
No, a ref struct X parameter can never be null, you're not referencing the struct you're referencing the variable holding the struct. You can't compile code that references no variable when calling that method, hence no need.
If you were to use pointers, then yes, nulls can occur even with pointers to structs, but not with ref parameters.
Because struct in C# is a value-type, it cannot be null. You can use Nullable<YourStruct> and then use HasValue property to check if it's just initialized.
And a struct is always initialized. if not by you, then by the compiler with default values.
I started my journey with C# but I realised that I have some problems with some basic information about memory when it comes to declaration of variables. See if I am correct.
int x; // I declared variable of type int, which name is x. Compiler will provide memory for it but we dont have known value of it.
x=10; // Now memory location is still the same but value now kept there is 10;
public struct Point {
public int x, y;
}
Now I define a struct named Point. Beacuse struct is a value type, it again has reserved memory for it on the computer. Howewer x and y have no value.
Now Point p1 = new Point(); // what is happening here? Struct is not a reference type. So is this just initialization of Point variable with the default constructor without assigning values to x and y?
Second short question. When I write a code like:
int x = 10;
Can I say that I created instance of class integer which value is 10 and name x;
I would be grateful for help.
// what is happening here? Struct is not a reference type. So is this just initialization of Point variable with the default constructor without assigning values to x and y?
No; there are 4 possible scenarios here:
a class: the memory space is wiped to all 0s, then any custom constructor is invoked, which may also involve field initializers
a struct called without a custom constructor: the memory space is wiped to all 0s
a struct called with a custom constructor: the custom constructor is required to assign all the fields
a struct variable used without ever calling a constructor: this is actually a thing, but the calling code must write to all the fields before they can do anything else with it; since most structs do not expose their fields, this rarely works
Second short question. When i write a code like:
int x = 10;
Can i say that i created instance of class integer which value is 10 and name x; I would be grateful for help.
Not really, because in C# terms, int is not a class (it might be in IL terms). Simply: you have declared a local variable of type int with name x and assigned it the value 10, if this is in a method. If this is a class field, then: you have declared a private instance field of type int named x with a field-initializer giving it the value of 10.
Incidentally, you should avoid public fields in general, and mutable fields on structs. You might prefer:
public struct Point {
private readonly int x, y;
public int X { get { return x; } }
public int Y { get { return y; } }
public Point(int x, int y) { this.x = x; this.y = y'; }
}
This will avoid a huge range of problems.
In C# the default struct constructor sets the struct memory to 0, effectively setting all variables to their default values.
In case of ints, it will be 0. For reference types, it will result in null.
(in other words, for any type T it will be default(T)).
Note that when you write a custom constructor in a struct, you must initialize all member fields.
When you write
int x;
this is similar to
Point p1 = new Point(); (considering Point structure is already defined)
in both the cases all integer variables will have default value of 0 and not null, which is is basically what is used in C# to denote 'nothing' and can be assigned only to reference types.
As well, in c# everything is a class, so when you write
int x = 10;
you are creating an instance of class Int32, though the run time will handle this as value type instead of ref type, as special case.
Same is true for other basic types like, Long, DateTime and few others
classes deal with the reference types and traditional data types deal with the value type just for example :
int i=5;
int j=i;
i=3 ; //then this will output i=3 and j=5 because they are in the different memory blocks .
Similarly if we talk about the object of a class say point class
class point
{
public int x,y;
void somefucnt(point p,int x)
{
Console.writeline("value of x is "+p.x);
x=22;
Console.writeline("value of x is "+p.x);
}
}
class someotherclass
{
static void Main(string [] args )
{
p1.x=10;
p1.somefunct(p1,p1.x);
}
}
Both console.write statements are printing 10 , despite ive changed x to some other value ? why is it so ?since p is just the reference to x so it should be updated by changing values of x . this thing is really confusing me alot .
The observed behavior has nothing to do with Value types vs Reference types - it has to do with the Evaluation of Strategy (or "calling conventions") when invoking a method.
Without ref/out, C# is always Call by Value1, which means re-assignments to parameters do not affect the caller bindings. As such, the re-assignment to the x parameter is independent of the argument value (or source of such value) - it doesn't matter if it's a Value type or a Reference type.
See Reference type still needs pass by ref? (on why caller does not see parameter re-assignment):
Everything is passed by value in C#. However, when you pass a reference type, the reference itself is being passed by value, i.e., a copy of the original reference is passed. So, you can change the state of object that the reference copy points to, but if you assign a new value to the reference [parameter] you are only changing what the [local variable] copy points to, not the original reference [in the argument expression].
And Passing reference type in C# (on why ref is not needed to mutate Reference types)
I.e. the address of the object is passed by value, but the address to the object and the object is the same. So when you call your method, the VM copies the reference; you're just changing a copy.
1 For references types, the phrasing "Call By Value [of the Reference]" or "Call by [Reference] Value" may help clear up the issue. Eric Lippert has written a popular article The Truth about Value Types which encourages treating reference values as a distinct concept from References (or instances of Reference types).
void somefucnt(point p,int x){
Console.writeline("value of x is "+p.x);
x=22;
Console.writeline("value of x is "+p.x);
}
Here, the x=22 won´t change p.x but the parameter x of (point p,int x)
Normally, your assumtion about values/references is ok (if I understood it correctly).
Tip: Google for c# this instead of passing a object to it´s own method
You change the value of the parameter (x), not the value of p.x, value types are passed by value unless you use the ref keyword.
Like in your first example, there is no relationship between i and j as well as the parameter x, and p1.x.Each variable has it's own space in the memory.So changing one of them doesn't affect to the other.
You have two different variables named x in the somefucnt function. One is the member variable x which you are trying to change, the other is the function input parameter in void somefucnt(point p, int x). When you say x = 22, the input parameter x is changed instead of the member variable x.
If you change the line x = 22 to this.x = 22 then it should work as you expect.
Side note:
A good practice to avoid confusion is to always have class members private and name them as _x. Otherwise, have public auto properties in CamelCase, like this:
public int X { get; set; }
These methods avoid ambiguity between class variables and function input variables.
I wrote two lines of code
1)
int i ;
MessageBox.Show(i.ToString());
2)
string str;
MessageBox.Show(str.ToString());
As I didn't initialize any of the variables and (1) is returning 0 but (2) throws an exception saying Object reference not set to an instance of an object
I can't understand that string is value type as int then this error is being shown.
....I can't understand that string is value type as int then this error is being occurred.
Who said string is value type? Its reference type and you have to give its value before using it, other wise you will get the error you are getting.
Whereas int is value type. Even if you do not initialize it, the are initialized by 0.
You can do this
string str = String.Empty;
str = SomeFunctionReturningString();
if (!String.IsNullOrEmpty(str)) //cause that function can also return null.
MessageBox.Show(str);
int is a value type, and string is a reference type, so they are different.
When you have a field (class-level variable), and you don't initialize the field value where you declare the field, then an int will be initialized with its default value which is 0. And a string field will get the default value of a reference type, which is the null reference.
When you have a local variable, you must assign a value to it before you use it. A local variable is one that is declared in the body of a method (or constructor, accessor etc.). Therefore your question was confusing, because it was not clear if you had fields or locals.
When declaring an int..
int A = 10;
why not do the following instead?
int A = new Int()
A=10;
are both the same?
Because int is syntax sugar for Int32 which is a value type. Incidentally, so is the constant value 10 (an instance of the value type Int32). That's why you don't need to use new to create a new instance, but rather making a copy of 10 and calling it A. And similar syntax works with reference types as well, but with the difference that a copy isn't made; a reference is created.
Essentially, you can think of 10 as a previously declared instance of Int32. Then int A = 10 is just setting variable A to a copy of value 10 (if we were talking about reference types then A would be set to a reference to the instance instead of a copy).
To better illustrate here's another example:
struct SomeValueType {
public SomeValueType(){
}
}
public static readonly SomeValueType DEFAULT = new SomeValueType();
Then you can just do this:
SomeValueType myValueType = DEFAULT; // no neeed to use new!
Now imagine that SomeValueType is Int32 and DEFAULT is 10. There it is!
You may have seen Java, where int and Integer are two different things, and the latter requires you to write new Integer(10).
In C# int is a special alias for Int32, and for all intents and purposes they are the same. Indeed, to create a new instance of any type you'd have to write new Int32() or something.
However, because integers are primitive types in C# (and most programming languages), there is a special syntax for integer literals. Just writing 10 makes it an Int32 (or int).
In your example you are actually assigning a value to the a variable twice:
int a = new Int32(); // First assignment, a equals 0
a = 10; // Second assignment, a equals 10
You might imagine that since the second assignment overwrites the first, the first assignment is not required.
In C# there are two kinds of types, "reference types" and "value types". (Pointers are a third kind of type but let's not get into that.)
When you use the default constructor of a value type, all you are saying is "give me the default value of this value type". So new int() is neither more nor less than just saying 0.
So your program is the same as:
int i = 0;
i = 10;
if you write youe code like
int A = new Int();
the variable 'A' is assigned by the default value of int, so you can use variable 'A' without assigning a value to it(in c# we cant use a variable without assigning a value to it)
when using the keyword new it will automatically call the default constructor, it will assign default values to the variables.
int A = new Int();
It declares and initializes A to 0.
Basically, the new operator here is used to invoke the default constructor for value types. For the type int, the default value is 0.
It has the same effect as the following:
int A = 0;