Thursday 9 April 2015

Introduction to C# programming in .NET

The first things you need to do are start a command line session and create some directories to store your sample programs in.To start a new command line session, click Start on the lower-left corner of the screen. Select the Run menu option from the pop-up menu.The Run dialog box will appear.Type cmd in the edit box and click OK.



Cammand prompt panel
command line window

Now you will create directories to save your C# programs in.You can set up any directory structure you like, but for purposes of this example, we use a structure that uses an abbreviated book title as the root directory, the chapter as a sub-directory, and the program name as the lowest level directory.

1. Type md C#.NET at the command prompt and press Enter.
2. Type cd C#.NET and press Enter to navigate to the C# .NET
directory.
3. Type md chap1 and press Enter to create a subdirectory called chap1.
4. Type cd chap1 and press Enter to navigate to the chap1 directory.
5. Type md FirstCSharpProgram and press Enter.
6. Type cd FirstCSharpProgram

You have now created the directory to store your first C# program, which will be called FirstCSharpProgram. Leave the command-line window open. You will use it to compile your first program a little later.

Creating Your First C# Program

using System;
namespace FirstCSharpProgram
{
/// <summary>
/// My first C# class. Contains the program entry point.
/// </summary>
class FirstCSharpClass
{
static void Main( string[] args )
{
try
{
/*
* Show when we wrote our first program on screen.
*/
DateTime today = DateTime.Now;
Console.WriteLine( "I wrote my first C# program at: " +
today.ToString() );
if ( args.Length > 0 )
{
// Show an optional message on screen.
string msg = "You wanted to say: " + args[0];
Console.WriteLine( msg );
}
}
catch ( Exception exception )
{
// Display any errors on screen
Console.WriteLine( exception.Message );
}
}
}
}

The command line compiler included in the Microsoft. NET Framework
SDK is named csc.exe.To compile the application, type csc.exe /out: FirstCSharpProgram.exe FirstCSharpProgram.cs on the command line
and press Enter. If you typed the program correctly, no errors should display. 


 
                     
                                                                                                                               

No comments:

Post a Comment