String & int Initialization question

  • Thread starter Thread starter Trevor
  • Start date Start date
T

Trevor

If I define a class like so:

class foo
{
private string bar;
private int x;
}


Is it redundant to do this in the constructor:

public foo()
{
bar = null;
x = 0;
}

I guess what I am asking is - Is a string initialized to null or
string.Empty? Is an integer initialize to 0?
 
Trevor,

The rule is that for reference types, the reference is set to null, for
value types, all fields of the instance are zeroed out, or set to null. So,
for strings, since it is a reference type, the value is null, not
string.Empty.

Hope this helps.

i
 
yes.
Basically, all objects (reference types, one of which is System.String) are
set to null, numbers are set to 0 and bools are set to false.

Simon
 
Hi Trevor,

Does the community's reply make sense to you?

Please feel free to feedback, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top