C#
  Home arrow C# arrow Page 3 - The Basics of C#: A "HelloWorld"...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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#

The Basics of C#: A "HelloWorld" Application
By: James Yang
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 29
    2002-01-09

    Table of Contents:
  • The Basics of C#: A "HelloWorld" Application
  • Hello World Example
  • HelloWorld source code explained
  • Simple C# language features
  • Conclusion

  • 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


    The Basics of C#: A "HelloWorld" Application - HelloWorld source code explained


    (Page 3 of 5 )

    The HelloWord sample we created on the last page is fairly elementary, yet precisely outlines the structure of a simple C# application:

    using library_name;

    namespace namespace_name

    {

    class class_name

    {

    static void Main([string[] args])

    {

    // Application code goes here

    }

    }

    }


    Let's take a look at what each these lines mean in relation to our C# application as a whole:

    using library_name;

    C# is based on the concept of using namespaces. Namespaces separate code into individually named-containers and avoids the chance of having duplicate variable names within the scope of a block. The "using" command allows us to specify which libraries namespaces we want access to. "using" commands are in no way similar to C++'s "#include" directive, and do not physically include any other source code into our file, they simply give us access to the namespaces available in the libraries that we have specified.

    The "System" library contains the namespaces for all common .NET variables such as System.Int32 (which represents an integer) amongst other things.

    Note: For any language to be compatible with .NET, it must provide implementations of the base .NET variable types. These include Int32, Int64, etc, C# provides its own version of these base variables in a form that resembles C++'s variable types: ushort, int, string, etc.

     If we reference the System library with a "using" command at the top of our source file like this:

    using System;

    ... and want to create a new integer variable, then we don't need to explicitly declare Int32 as part of the System namespace, because we already have access to the system libraries namespace:

    Int32 myNum = 10;

    On the other hand, if we didn't include a "using" command to reference the system library, then we would have to explicitly specify which namespace the Int32 class resides in, like this:

    System.Int32 myNum = 10;

    Although .NET contains several pre-defined namespaces, we can create our own using a namespace block, like this:

    namespace myNS

    {

    System.Int32 x = 10;

    }


    This would create a new namespace named "myNS" under the global section of our C# application. To reference the integer variable x from within our new "myNS" namespace, we would use the following syntax:

    myNS.x;

    To access variables, objects, or other namespaces (namespaces can be nested) from within a namespace, you call the namespaces in the order that they are nested and separate each one with a dot operator. If we have three nested namespaces like this:

    namespace ns1

    {

    namespace ns2

    {

    namespace ns3

    {

    System.Int32 number;

    }

    }

    }


    ... then we can retrieve the value of the "number" integer with a command like this:

    ns1.ns2.ns3.number;

    Although a namespace isn't required in our sample application, it helps to physically differentiate our code from any other code that might be present in the same source code file.

    class class_name

    {

    }


    Just like in C++, C# uses classes to facilitate its object-orientated design methodology. Classes allow us to create individual self-contained units of code, which can be instantiated throughout our application. All C# applications must contain at least one class. If we wanted to create a class named "myClass", then we would use the following commands:

    class myClass

    {

    // Methods and members for this class go here

    }


    All C# applications must have a Main() function, which is the entry point for execution of the application:

    static void Main([string[] args])

    {

    // Application code goes here

    }


    As you can see, the "string[] args" argument list is optional, and can be used to access the parameters passed to your executable from the command line. The Main() function is declared as public by default and can return either an integer variable type or nothing, with "void". To explicitly specify our Main() function as public and to return an integer variable, we would use the following Main() function:

    public static int Main()

    {

    }

    More C# Articles
    More By James Yang


     

    C# ARTICLES

    - Introduction to Objects and Classes in C#, P...
    - Visual C#.NET, Part 1: Introduction to Progr...
    - C# - An Introduction
    - Hotmail Exposed: Access Hotmail using C#
    - Razor Sharp C#
    - Introduction to Objects and Classes in C#
    - Making Your Code CLS Compliant
    - Programming with MySQL and .NET Technologies
    - Socket Programming in C# - Part II
    - Socket Programming in C# - Part I
    - Creational Patterns in C#
    - Type Conversions
    - Creating Custom Delegates and Events in C#
    - Inheritance and Polymorphism
    - Understanding Properties in C#







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