Initializing variables?

A

Anders Eriksson

Coming from C++, where if you don't initialize your variables you're
screwed, I wonder how this works in C#.

Should I initialize variables?
E.g.
string foo = string.Empty;
int i = 0;
double n = 0.0;
BarClass bar = null;

If I don't initialize them what value will they have?

// Anders
 
B

bradbury9

El jueves, 20 de septiembre de 2012 08:35:05 UTC+2, Anders Eriksson escribió:
Coming from C++, where if you don't initialize your variables you're

screwed, I wonder how this works in C#.



Should I initialize variables?

E.g.

string foo = string.Empty;

int i = 0;

double n = 0.0;

BarClass bar = null;



If I don't initialize them what value will they have?



// Anders

--

English isn't my first language.

So any error or strangeness is due to the translation.

Please correct my English so that I may become better.

It depends on the variable type:

Class instances will be NULL.
Primitive types behave different. You can check which types are primite andwhat value will they have here http://msdn.microsoft.com/en-us/library/aa711900(v=vs.71).aspx
Other types will be NULL
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top