Blog
Archive
<January 2011>
SunMonTueWedThuFriSat
2627282930311
2345678
9101112131415
16171819202122
23242526272829
303112345
Monthly
Go
Search
  |  
Author: Created: 11/18/2010 9:57 AM RssIcon
Sargolas geeky stuff
By Michael Seirer on 1/20/2011 11:45 AM

Today I trippled over a very interesting change between .NET 3.5 and .NET 4.0:

I conducted a code review and found the following code:

 

   1: var message = new MailMessage();
   2: message.From = new MailAddress("...");
   3: message.To.Add(new MailAddress("..."));
   4: message.Subject = "...";
   5: message.Body = "...";
   6:  
   7: var client = new SmtpClient("host");
   8: client.Send(message);

 

I remembered to use SmtpClient in a using statement to trigger IDisposable properly. But to my great surprise I found out, that in .NET 3.5 SmtpClient doesn’t implement IDisposable, in .NET 4.0 it does!

 

This is especially interesting because you don’t get any compiler notice if you migrate from 3.5 to 4.0…

 

Though, if you have enabled FxCop running (Enable Code Analysis on Build) even with the smallest set of rules from Microsoft they do give you the hint to use usig:

Capture

 

Btw: MailMessage is implementing IDisposable in 3.5 and 4.0.

By Michael Seirer on 1/5/2011 11:48 AM

lets start with a simple class for demonstration purposes – note, that the DateOfBirth is nullable …

   1: public class Person
   2: {
   3:     public string Name { get; set; }
   4:     public DateTime? DateOfBirth { get; set; }
   5: }

 

using object initializers, we can instantiate the class in the following way:

   1: // works
   2: Person p1 = new Person() { Name="Michi", DateOfBirth=DateTime.Now };

 

but what happens, if we want to do the initialize the values based on some condition? you could be tempted to write the following code:

   1: // doesnt compile
   2: // Type of conditional expression cannot be determined because there is no implicit conversion between ''
   3: bool someCondition = false;
   4: Person p2 = new Person()
   5: {
   6:     Name = "Michi",
   7:     DateOfBirth = (someCondition == true ? null : DateTime.Now)
   8: };

 

boom! you get a compile error:

compile_error

 

interestingly enough, an assignment from null to DateTime? is working!

   1: // but this works...
   2: Person p3 = new Person();
   3: p3.Name = "Michi";
   4: if(someCondition == true)
   5:     p3.DateOfBirth = null;
   6: else
   7:     p3.DateOfBirth = DateTime.Now;

 

hmmm… so how do we get to use object initializers for elegante syntax but still are able to assign nullable types? fallback to the “real” type

that is used behind the scenes if you declare a DateTime? – it’s an instance of the System.Nullable struct – see Nullable Types C# on MSDN.

 

so here is the working code:

   1: // in order to still use object intializers you can use the "real" type of DateTime?
   2: Person p4 = new Person()
   3: {
   4:     Name = "Michi",
   5:     DateOfBirth = (someCondition == true ? new Nullable() : DateTime.Now)
   6: };
By Michael Seirer on 1/2/2011 11:57 PM

just sent my old homepage into retirement and created a fresh one with flavors.me – kinda neat to configure and flexible enough for me to make a nice hub of my digital activities. nothing more, nothing less – enjoy www.sargola.com.