C#
  Home arrow C# arrow Page 10 - Introducing C# and the .NET Framework
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Introducing C# and the .NET Framework
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 87
    2005-01-31

    Table of Contents:
  • Introducing C# and the .NET Framework
  • The .NET Framework
  • C# Source code
  • The .NET Type Story
  • A World of .NET
  • The Common Language Runtime
  • The Common Type System
  • A World of Interoperability Through the CLS
  • Where Can I Find the .NET SDK?
  • Writing the Hello World Example with the C# Compiler
  • Assemblies
  • Analyzing the Code

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Introducing C# and the .NET Framework - Writing the Hello World Example with the C# Compiler


    (Page 10 of 12 )

    Open your favorite text editor (I'm using notepad in this example) and copy the following C# code into the file. Save it as Hello.cs. The cs extension stands for C Sharp.

    using System;

    namespace FirstApplcation
    {
      
       class Class1
       {
          static void Main()
          {
             //the Console.WriteLine() method takes a string as
             // a parameter and write it to the console
             Console.WriteLine("Hello World, I like C#");
          }
       }
    }

    You need to compile the Hello.cs file using the C# compiler with the following command from the command prompt:

    csc Hello.cs

    The C# compiler will compile the application and you would see the version of the compiler in the command prompt. This means that everything is fine. Run the application using the command prompt and you will get the following message: "Hello World, I like C#."

    Writing the Hello World example with VS.NET

    Now we will create the Hello World example using Visual Studio.NET (VS.NET) so you can see the power of VS.NET and its IDE (Integrated Development Environment).

    Start VS.NET and select File --> New --> Project. The New Project windows will be shown. Choose Visual C# Projects from the Project Types pane and choose Console Application from the Templates pane.

    Introducing C# and the .NET Framework

    Notice that Visual Studio.NET automatically save the project files under

    C:\Documents and Settings\Michael\My Documents\Visual Studio Projects

    where Michael is the current user that I logged in with. Under the Visual Studio Projects Folder you will find a folder named FirstApplication (as the name that we will select for the Project). Write FirstApplication in the Name textbox and click on OK. VS.NET will create the needed files to create the project for you. Open the Solution Explorer (if it's not already opened) using the combination CTRL + ALT + L , and you will find some files created automatically for you. Since this is not an article about VS.NET, I will not go into further details about this tool.

    Here's the automatically generated C# code that lives in the only generated C# class file which always called Class1.cs:

    using System;

    namespace FirstApplication
    {
       ///
       /// Summary description for Class1.
       ///
       class Class1
       {
          ///
          /// The main entry point for the application.
          ///
          [STAThread]
          static void Main(string[] args)
          {
             //
             // TODO: Add code to start application here
             //
          }
       }
    }


     
    VS.NET is a smart development environment; it generates a lot of code on your behalf, especially if you are working with Windows-based applications and Web-based applications. When you add a button to the form and double-click on the button to write an event handling code, VS.NET will generate an event handler method and register it with the event delegate (it's the .NET mechanism to fire event which we will spend some time on). Because Console-based applications have a limited functionality, VS.NET generates only a class with some comments, along with the Entry Point of the application.

    Delete the comment inside the Main() method and add this line of code:

    Console.WriteLine("Hello World, I like C#");

    While you type the above line of code, VS.NET Intellisense's feature offers you a way to expose all the available Console class members. This feature makes our development easier, because we get a list of all the available members for a certain type.

    We will not invoke the C# compiler to compile the application; we will just click the Debug --> Start Without Debugging menu option or press the CTRL + F5, and VS.NET will compile the application and save it under the bin\Debug folder inside the application folder (which in this case is FirstApplication). The application will automatically execute, and you will get the same message "Hello World, I like C#" as you did in the compiler version, with one difference. We started the compilation process without going into debug mode, so when the message outputs to the Command Prompt, the application will terminate and we will not be able to see it. We can modify the code to wait until we press the Enter key, then terminate. Add the following line of code

    Console.ReadLine();

    This line of code will force the Command prompt to wait until it reads a line (in this case we will press the enter key only) and then terminate. This time run the application using the F5 key only; it will compile and run as expected. You will get the message "Hello World, I like C#" and the Command Prompt will wait until you press the enter key, then terminate the execution.

    Although we will have a complete article about .NET Assemblies and Namespaces later in this series, you need to know what they are before we analyze the program code.

    More C# Articles
    More By Michael Youssef


     

    C# ARTICLES

    - Coding a CRC-Generating Algorithm in C
    - Cyclic Redundancy Check
    - Handling Methods and Functions
    - Destroying Objects in C#
    - Creating Objects in C-Sharp
    - Classes and Objects
    - Programming Languages: Managed versus Native
    - LINQ-to-MySQL with DbLinq in C#
    - Working with Dates and Times in C#
    - Generics, Dictionaries, and More
    - More About Generics
    - Working with C# Collections
    - Generics
    - C# and XML
    - Pointers and Arrays in C#





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek