Value types

18 09 2008

Ok so going right back to basics here. Value types in C#. Even the ones listed as most used, I don’t really tend to use them all that much. The ones I tend to use are:

  • Int32 (declared Int32 i = 0;)
  • Double (declared double d = 0;)
  • Boolean (declared bool b = false;)
  • DateTime (I rarely find this needs to be declared as a variable. I tend to assign it to string values. E.g. string s = s.DateTime.Now.ToString();)

An interesting function I often forget about which can be very useful, is using nullable variables. This allows you to check that a required variable has been set. For example:

Nullable<bool> b = null;
if(b.HasValue)Console.WriteLine("b is {0}" , b.Value);
else Console.WriteLine("b is not set.");

So back to basics indeed, but very important things to be aware of!




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: