Ziff Davis EnterpriseHeader
Advertisement
Advertisement
Advertisement
Friday, August 10, 2007 10:53 AM/EST

Compiling the C# samples at the command line

If you want to try out some of the Visual Studio 2008 code (such as the ones in my recent LINQ article) without starting up Visual Studio, you can do so at the command prompt.  Visual Studio always includes a command prompt, which is really just a DOS command prompt with environment variables set up. If you've installed the Visual Studio 2008 Beta, you can find it in the Programs menu at Microsoft Visual Studio 2008 Beta 2 -> Visual Studio Tools -> Visual Studio 2008 Beta 2 Command Prompt.

But what if you don't want to install the huge Visual Studio? You can get by with just installing the .NET runtime. But then you need to set up your command prompt manually. Really, this just involves adding the framework to your system path. You certainly know of different ways to do that; one is to create a batch file with this in it:

set path=C:\Windows\Microsoft.NET\Framework\v3.5;%PATH%

Then you can easily compile your code. Try this: Create an empty directory, and create a file called linq1.cs. Put the following in the file using your favorite text editor. (I personally like and recommend the free SciTE, found at http://www.scintilla.org/SciTE.html.)

(This code is from the LINQ article.)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LINQConsole1
{
    class Program
    {
        public static void Linq1()
        {
            int[] numbers = { 5, 10, 15, 20, 25, 30, 35, 40 };
            var myNums =
                from n in numbers
                where n > 25
                select n;
            foreach (var x in myNums)
            {
                Console.WriteLine(x);
            }
        }
       
        static void Main(string[] args)
        {
            Linq1();
        }
    }

Save this file and head back to the command prompt. Now you can compile it like so:

C:\dev\tests\Orcas>csc linq1.cs
Microsoft (R) Visual C# 2008 Compiler Beta 2 version 3.05.20706.1
for Microsoft (R) .NET Framework version 3.5
Copyright (C) Microsoft Corporation. All rights reserved.
C:\dev\tests\Orcas>

Done! Now you can run it:

C:\dev\tests\Orcas>dir
 Volume in drive C is SQ004464V01
 Volume Serial Number is EEC3-155E
 Directory of C:\dev\tests\Orcas\
08/10/2007  10:50 AM    <DIR>          .
08/10/2007  10:50 AM    <DIR>          ..
08/10/2007  10:33 AM               598 linq1.cs
08/10/2007  10:50 AM             4,608 linq1.exe
               2 File(s)          5,206 bytes
               2 Dir(s)  106,863,243,264 bytes free
C:\dev\tests\Orcas>linq1.exe
30
35
40
C:\dev\tests\Orcas> 

 

 

TrackBack

TrackBack

http://blogs.devsource.com/cgi-bin/mte/mt-tb.cgi/11503

Post a Comment

 
 
Advertisement

Syndication

Subscribe: