advanced web statistics

C# Developers Finally Got Their 'with' Operator

2/27/2008 5:06:44 PM

I remember 3 years ago I used to work with a guy who was very adamant about doing ALL development work in C#.  He was the one that actually turned me on to writing classes in C# then using Reflector to disassemble the class and view in VB.NET, copy into a VB.NET class, and include in a project where the code HAD to be VB.NET.  The reason for this was that it was faster to write C# code than it was to write VB.NET code since he hated it so much.

This guy was ex-Microsoft and very, very passionate.  He once mentioned on a developer conference call that he would rather be "waterboarded at Guantanamo than develop in any form of Visual Basic" and that "the Micrsoft Access team should be fired and the project nuked because it points and laughs at the face of REAL database developers being that it is a sorry excuse for a database."  Those are direct quotes. Did I mention he was passionate?

Anyway, he did mention that there is one aspect of Visual Basic .NET that he wishes C# had: the WITH operator.  If you haven't seen the with operator here is a snippet:

Dim stream As MemoryStream = New MemoryStream
Dim writer As XmlTextWriter = New XmlTextWriter(stream, Encoding.UTF8)

With writer
   .Formatting = Formatting.Indented
   .Indentation = 5
End With 

This is syntanctical sugar for not having to write out writer. on each line when setting the properties of the XmlTextWriter. Could save some time.  That code, when compiled and analyzed in Reflector actually looks like:

Dim stream As New MemoryStream
Dim writer As New XmlTextWriter(stream, Encoding.UTF8)
Dim VB$t_ref$L0 As XmlTextWriter = writer
VB$t_ref$L0.Formatting = Formatting.Indented
VB$t_ref$L0.Indentation = 5

Not surprising.  In case you're wondering I'm using the Class View add-in for Reflector and that is how I get the colors above. It seems they are a bit off. That's ok.  You can still see that where we used the with operator we have a new instance of the XmlTextWriter and set the properties accordingly.

Fast forward to C# and a console application created in VS 2008.  I was sandboxing and dinking with automatic properties and looked at some code from a 2.0 project recently converted that dealt with an XmlTextWriter and noticed that ReSharper displayed a yellow lightbulb asking if I wanted to use an object initializer. How about yeah? I'd seen this before when I first started playing with Orcas Beta 2 last year.  I hadn't paid much attention to it since I've been extremely busy since then!  Same code above in C#:

MemoryStream stream = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8)
{
   Formatting = Formatting.Indented,
   Indentation = 5
}; 

Just like using with I am able to populate pertinent properties for my XmlTextWriter.  This could be all on one line but I broke it out for readability.  I also stripped out (as is the case above with VB) all using constructs.

That C# code in Reflector:

MemoryStream stream = new MemoryStream();
XmlTextWriter <>initLocal0 = new XmlTextWriter(stream, Encoding.UTF8);
<>initLocal0.Formatting = Formatting.Indented;
<>initLocal0.Indentation = 5;
XmlTextWriter writer = <>initLocal0

Very similar to the VB.NET with reset.  I am not saying that is the same thing.  With using the VB.NET with you still have to instantiate your object and break out the with on a separate line. You notice that the original instance of XmlTextWriter is set to the newly-created instance. This is the opposite of the case above with VB.  So in a round-about way, all those C# developers from the VB background that complained about not having with finally got what they were looking for.  I know that with the new object initializer syntax there are more frills.  Take for example nested object initializers in the case where one of your properties is another object.

Foo foo = new Foo()
{
   FooId = Guid.NewGuid(),
   Address = new Address()
               {
                  Street = string.Empty(),
                  City = "Seattle",
                  State = "WA"

               };
};

C#

kick it on DotNetKicks.com

Comments


Yeah, the new object initializers are sweet (and assumingly a byproduct of LINQ).

I remember writing classic ASP code with:

with Response
.Write("Stuff")
.Write("MoreStuff")
end with

Unfortunately, I haven`t found a way to kick things off methods like that in C#, which is probably a good thing as we already have our using statement and can accomplish nearly the same thing.

Posted by: David R. Longnecker | 2/28/2008 5:16:00 PM

I`m starting to do more production stuff in 3.5 now. I like it!

ps. installing build R# build 740 now :: crosses fingers ::

Posted by: Will Asrari | 2/28/2008 5:18:09 PM

We now have all production applications running in 3.5. The transition was almost painless. We had a few applications that needed LINQ updates (InsertOnSubmit, thank you Microsoft for changing it >_>) and a library that I had build using the new directory services for Active Directory, but past that--clean and smooth.

Heh, I tossed 741 on this morning. Since JB is so far behind/ahead of me, I`ve made installing my early morning ritual (which does, of course, lead to disaster if it doesn`t work--but it keeps things exciting and gives me an excuse to not screw with MOSS so early in the morning).

btw: love the new avatar, LOL`d.

Posted by: David R. Longnecker | 2/29/2008 9:27:28 AM

Leave a Comment

   

  Enter the text to proceed!