Tuesday, January 18, 2011

Example C# code to learn computer programing

Example:
int foo = 42;         // Value type.
object bar = foo;     // foo is boxed to bar.
int foo2 = (int)bar;  // Unboxed back to value type.

Preprocessor

C# features "preprocessor directives"[24] (though it does not have an actual preprocessor) based on the C preprocessor that allow programmers to define symbols but not macros. Conditionals such as #if, #endif, and #else are also provided. Directives such as #region give hints to editors for code folding.
public class Foo
{
    #region Procedures
    public void IntBar(int firstParam) {}  
    public void StrBar(string firstParam) {}  
    public void BoolBar(bool firstParam) {}  
    #endregion
 
    #region Constructors
    public Foo() {}
    public Foo(int firstParam) {}
    #endregion
}

Code comments

C# utilizes a double forward slash (//) to indicate the rest of the line is a comment. This is inherited from C++.
public class Foo
{
    // a comment
    public static void Bar(int firstParam) {}  //Also a comment
}
Multi-line comments can be indicated by a starting forward slash/asterisk (/*) and ending asterisk/forward slash (*/). This is inherited from standard C.
public class Foo
{
    /* A Multi-Line
       comment  */
    public static void Bar(int firstParam) {}  
}

[edit] XML documentation system

C#'s documentation system is similar to Java's Javadoc, but based on XML. Two methods of documentation are currently supported by the C# compiler.
Single-line documentation comments, such as those commonly found in Visual Studio generated code, are indicated on a line beginning with ///.
public class Foo
{
    /// <summary>A summary of the method.</summary>
    /// <param name="firstParam">A description of the parameter.</param>
    /// <remarks>Remarks about the method.</remarks>
    public static void Bar(int firstParam) {}
}
Multi-line documentation comments, while defined in the version 1.0 language specification, were not supported until the  1.1 release. These comments are designated by a starting forward slash/asterisk/asterisk (/**) and ending asterisk/forward slash (*/).
public class Foo
{
    /** <summary>A summary of the method.</summary>
     *  <param name="firstParam">A description of the parameter.</param>
     *  <remarks>Remarks about the method.</remarks> */
    public static void Bar(int firstParam) {}
}
Note there are some stringent criteria regarding white space and XML documentation when using the forward slash/asterisk/asterisk (/**) technique.
This code block:
/**
 * <summary>
 * A summary of the method.</summary>*/
produces a different XML comment from this code block:
/**
 * <summary>
   A summary of the method.</summary>*/
Syntax for documentation comments and their XML markup is defined in a non-normative annex of the ECMA C# standard. The same standard also defines rules for processing of such comments, and their transformation to a plain XML document with precise rules for mapping of CLI identifiers to their related documentation elements. This allows any C# IDE or other development tool to find documentation for any symbol in the code in a certain well-defined way.

Libraries

The C# specification details a minimum set of types and class libraries that the compiler expects to have available. In practice, C# is most often used with some implementation of the Common Language Infrastructure (CLI), which is standardized as ECMA-335 Common Language Infrastructure (CLI).

"Hello, world" example

The following is a very simple C# program, a version of the classic "Hello, world" example:
using System;
class ExampleClass
{
    static void Main()
    {
        Console.WriteLine("Hello, world!");
    }
}
The effect is to write the following text to the output console:
Hello, world!
Each line has a purpose:
using System;
The above line of code tells the compiler to use 'System' as a candidate prefix for types used in the source code. In this case, when the compiler sees use of the 'Console' type later in the source code, it tries to find a type named 'Console', first in the current assembly, followed by all referenced assemblies. In this case the compiler fails to find such a type, since the name of the type is actually 'System.Console'. The compiler then attempts to find a type named 'System.Console' by using the 'System' prefix from the using statement, and this time it succeeds. The using statement allows the programmer to state all candidate prefixes to use during compilation instead of always using full type names.
class ExampleClass
Above is a class definition. Everything between the following pair of braces describes ExampleClass.
static void Main()
This declares the class member method where the program begins execution. The .NET runtime calls the Main method. (Note: Main may also be called from elsewhere, like any other method, e.g. from another method of ExampleClass.) The static keyword makes the method accessible without an instance of ExampleClass. Each console application's Main entry point must be declared static. Otherwise, the program would require an instance, but any instance would require a program. To avoid that irresolvable circular dependency, C# compilers processing console applications (like that above) report an error if there is no static Main method. The void keyword declares that Main has no return value.
Console.WriteLine("Hello, world!");
This line writes the output. Console is a static class in the System namespace. It provides an interface to the standard input, output, and error streams for console applications. The program calls the Console method WriteLine, which displays on the console a line with the argument, the string "Hello, world!".

No comments:

Post a Comment