August 2007 - Posts

Compiling C# code from the command line
Fri, Aug 31 2007 12:13

It's well known that the C# compiler is the csc.exe and that you can find it on the installation folder of the .NET platform. What i've seen in these last days (specially since i've started my in-depth study of the C# language and have started discussing it with some collegues and friends) is that many people use the C# compiler and really don't know what it does or what are the available options you can use when you compile a C# app.

For instance, i was astonished with the number of people who don't know what the following line does:

csc /target:exe /reference:mscorlib.dll /out:HelloWorld.exe  HelloWorld.cs

Maybe this is a classical example of why VS rottens the mind (and no, i won't stop using VS - specially now, that I'm a proud owner of a resharper's lic:))?  

Even among those that know what it does (btw, it's compiling the HelloWorld.cs file into an exe called HelloWorld.exe and saying that it needs the "all mighty" mscordlib.dll to do that), many don't really understand why you can reduce it simply to this (besides saying that it's magical):

csc HelloWorld.cs

So i thought I should write something about this (even though that i'm positive that there's already lots and lots of info out there). Ok, so why do the previous 2 instructions produce similar results? Easy:

  • by default, the csc.exe will  always generate an executable. since i was trying to built an exe, then i can just remove the /target:exe from the command;
  • mscorlib.dll is a special assembly since it contains several of the basic (or primitive) types that you use when you build a C# program. The C# compiler knows this and will always add a reference to this assembly. Notice that this might not be what you want (for instance, when you're building a silverlight application, you need to compile your cs files agains Silverlight's mscorlib.dll and not against the one that is installed with the full .NET version). In these scenarios, you can cancel the default behavior of the compiler by passing the /nostdlib parameter;
  • since i wanted the assembly to have the same name as the file that i was compiling, then it's safe to discard the /out parameter. When you do that (ie, when there's no /out parameter), the compiler will name the final assembly with the name of the file that has the Main method (exe) or the name of the first cs file you're compiling (dll).

Not too complicated, right? More dificult is understanding why you can compile an app that uses a type defined inside the System.Data.dll assembly without passing a reference to that assembly during compilation. well, it happens that the csc.exe can also receive a response file. A response file is simply a text file where you define a switch per line. For instance, if i wanted, i could transform my first example in a helloworld.rsp (notice the extension) file that would look like this:

/target:exe
/referente:mscorlib.dll
/out:HelloWorld.exe

You can make the compiler to use these settings by using the /@ switch:

csc @helloworld.rsp helloworld.cs

If you want, you can have several response files. One important thing: the C# compiler will always load any csc.rsp response files it finds. This means that you can simply rename the previous file to csc.rsp and the compiler will pick it up automatically, making the @ switch disposable(do keep in mind that in  my example you really don't need a response file since the parameters i'm passing are also automatically used  by the compiler, as explained in the previous paragraphs).

Ok, interesting right? but still doesn't answer my last question. It happens that the compiler currently searches for rsp files in two places: current working folder and the installation folder of the compiler. If you open that folder, then you'll see that it has a csc.rsp file with references to several of the assemblies installed by the .NET (including the assembly i mentioned in the previous example). This file is special and is always picked up by the compiler. That's why you can do what i was saying 2 or 3 paragraphs above...

One important gotcha: the csc.rsp file will always be loaded by the compiler. If you don't want that, then pass the /noconfig switch to the C# compiler.

btw, do notice that you can add or remove assemblies, though i see no reason for removing any of the list. Some people think that by removing assemblies they're improving the performance of the new compiled assembly. This is not true because the compiler is smart enough to only add references to types that are used in your C# code files.

Finally, another question that pops up quite frequently, is where do those assemblies come from? for instance, what happens when you have several versions of the .NET platform installed. To answer this, we must know the places where the C# compiler searches for the dll:

  1. working directory
  2. directory that contains the csc file (notice that inside this directory you'll find copies of the dlls that exist on the GAC.)
  3. folders passed through the /lib switch
  4. folders defined on the LIB ambient variable

As always (or should i say, as amost always?) msdn has a good reference on the parameters you can pass to the compiler. Reading them garantees that you're aware of all the options you can use when you compile your program. Don't stop using VS (or another cool editor). However, do know what you're doing!

by luisabreu | 6 comment(s)
Filed under:
LINQPad: cool!
Thu, Aug 30 2007 21:48

I was really looking for this sort of tool and I've found it via Scott Gu's post. Awesome!

by luisabreu | with no comments
Filed under:
C#: where does the name come from?
Thu, Aug 30 2007 20:40

This isn't really a technical post, so you might want to skip it if you're too busy :)

As I've said, i've been spending some time looking at C# and how it produces code that runs on the CLR. One of the things that has always puzzled me is why C# is called C# :) Until now, I've heard one or two interesting theories. The first is old (at least, I recall having heard about it in the past): since C# is supposed to be a better C++, then some see the # symbol as being composed by four + symbols.

The second one says that # is really the number sign that is used for a musical sharp, implying that he current sound is half step above the other (which really means that C# would above the other languages).

Btw, I 've found both these theories presented on the C# professional book (chapter 1 is available online here).

And you? what do you think? have you heard any better explanation for its name?

by luisabreu | 4 comment(s)
Filed under:
ASP.NET AJAX: Alessandro's book is out
Mon, Aug 27 2007 17:42

Alessandro Gallo's  (aka Garbin) book ASP.NET AJAX in Action is out and you can order it from here. I've met Alessandro on MSN during the early days of ATLAS and we've had several cool discussions about the platform. I'm sure he has done a fine job with this book. So, if you're looking for a good english book on AJAX, then go ahead and check this one here (btw, if you prefer to read in portuguese, then don't forget that i've also writen a book for  FCA  - sorry for the shameless publicity, but i just couldn't resist :) ).

by luisabreu | 3 comment(s)
Filed under: ,
Back at full speed
Mon, Aug 27 2007 9:56

After a three weeks vacations, i'm back at full speed and I've decided that this month will be the "C# month". what this means is that during this month (and the next) I'll be studying the C# language in depth and i'll try to share one or two things here.

i'm not sure about you, but i'm still not used to the end of vacations...

by luisabreu | 2 comment(s)
Filed under:
Subversion and reusing a previous repository folder
Wed, Aug 22 2007 21:49

I've been using Subversion for a year now. I'm really glad i've started using it and i really enjoy its working model (which is quite different from VSS default working model). Now, a few days ago, i decided to format my PC, which means that i had to install everything again. Now, the problem is how to reuse the old repository since i hadn't made any backup (and as always, i didn't read the docs).

Lucky for me, it was really simple since Subversion 1.4 + will already let you have a svn service for free (no need to install aditional updates, like happened in the old versions of the product - if this is not news for you, then you've got me! after installing it for the first time and having it work without any problems, i just left it there running without running any updates whatsoever). After installing, the only thing i had to do was create a service from the command line by using the sc utility. Now, this is where the fun begins.

As always, google was a good friend and returned this with detailed info on the command i was supposed to run:

create svn.local binpath= "\"c:\program files\subversion\bin\svnserve.exe\" --service --root D:\svnrepo" displayname= "Subversion Repository" depend= Tcpip

 

simple, right? well, not quite...i didn't noticed the blanks after binpath, depend and displayname and that was enough for the service not being created. if you forget those spaces, you'll just see it display the help, withouth any util info on what's going on. so, the next time you need to create a new entry on the services list, don't forget those nasting spaces...

by luisabreu | with no comments
Filed under:
Silverlight: understanding namescopes
Fri, Aug 17 2007 15:37

Today i was starting to write about silverlight namescopes but then I've found this cool post which talks about most of the things i was writing about. btw, i'd just like to add that when you use the XamlReader.Load method, you can also create a new namescope by using the overloaded version that receives 2 parameters (the second parameter lets you specify if a new namescope should be created)

by luisabreu | with no comments
Filed under:
How to test your Silverlight quickly
Fri, Aug 17 2007 15:32

When you create a Silverlight project, you'll automatically get an HTML file that you can open in the browser to run some quick tests. Unfortunately, the file is opened directly from the file system instead of being served from a web server. This means that some things don't work and you'll need to create a web site to test them (ex.: getting a video file and making it play on the MediaElement element).

Most of the time, I'm not developing Silverlight applications; i'm just writing some tests to see if something works as expected. in these cases, having to create a web application and adding pages to it might be a little bit overkill. So, instead of creating a web site and adding a Silverlight link to it (and chaning the properties of the HTML/js files  in order to get them automatically on the web site), I just add an existing web site that points to the folder that contains the Silverlight project.

Here's how you do it:

  1. create a new Silverlight project
  2. go to the file menu and choose add -> existing web site
  3. since you can use the file sytem server for testing these functionalities, just navigate until you get the folder that has the silverlight project created in 1
  4. click open

And that's it. you'll get a solution with 2 projects which are just different views of the same project. When I need to write some code, I just open the file from the Silverlight project. When i need to test it, I right click the HTML file on the Web project and choose the view in browser option.

by luisabreu | with no comments
Filed under:
Silverlight: first PT project goes live
Sat, Aug 11 2007 13:28

I've just noticed that the first portuguese Silverlight project has gone live. Media preview.tv was built with Silverlight 1.1 and shows some of the cool things you can do with it. if you have the time, check it out.

by luisabreu | with no comments
Filed under:
Si, si, si: i'm back!
Sat, Aug 11 2007 13:00

well, sort of. i'm back from my trip to the Canarias (Playa del Ingles, to be more precise) where I was able to spend some days sleeping on the beach and having lots and lots of fun. I'm still on vacations, so don't expect much movement around here on the next days.

by luisabreu | with no comments
Filed under:
How to be in 2 different places on the same time
Thu, Aug 2 2007 15:05

I'm sure that, like my friend Paulo, I'm missing something here...

by luisabreu | with no comments
Filed under:
Silverlight: working with text
Wed, Aug 1 2007 14:31

In the last days i've been busy...thinking about my vacations which are coming :)

I thought i'd write an article and submit it to a site, but i wouldn't have the time to format it. so, i've given up on the idea and i'm putting it here instead . The article presents the main features Silverlight gives you for working with text. There's also a sample that accompanies the article with several XAML pages that illustrate the features described on the docs.

Do notice that i've started writing this before the release of the 1.1 alpha refresh, so there may be one or two things which might not work correctly...

by luisabreu | with no comments
Filed under: