Hosted By:
Lunarpages.com Web Hosting
Choose Language:
Program Structure
                    using System;
                    
                    namespace Namespace1
                    {
                        public class Class1
                        {
                            public static main( string[] args )
                            {
                                int i = 0;
                                
                                if( args.length == 1 )
                                    i = args[0];
                                    
                                System.Console.Write( "You entered " + i + "\n" );
                            }
                        }
                    }    
                
Comments
                    //Single line Comment
                    
                    /*Multi
                      line Comment*/
                    
                    ///Single line xml Comment
                    
                    /**Multi line
                        xml Comment*/
                
Data Types
                    bool
                    byte, sbyte
                    decimal
                    float, double
                    short, ushort, int, uint, long, ulong
                    char
                    object
                    string
                
Arrays
                    int[] nums = {1,2,3};
                    Console.WriteLine("The array has " + nums.Length + "elements.");
                    nums[0] = 2;            //New array: 2,2,3
                    nums[3] = 5;            //Thows exception
                    
                    int[,] twoD = new int[rows,cols];
                    twoD[0,0] = 2;
                    
                    //Jagged array
                    int[][] jagged = new int[3][]{ new int[3]. new int[5], new int[3]};
                    jagged[1][4] = 5;
                
Operators
                
                    //Arithmetic
                    * / % + - 
                    
                    //Logical
                    && || ! & | ^
                    
                    //Bitwise
                    & | ^ ~ << >>
                    
                    //Assignment
                    = += -= *= /= %= &= |= ^= <<= >>=
                    
                    //Increment/Decrement
                    ++ --
                    
                    //Conditional operator
                    ?:
                
Decisions
                    if( x > 5 )
                        message = "More than five!";
                    else
                        message = "Less than five!";
                        
                    message = (x > 5) ? "More than five!" : "Less than five!";
                        
                    if( x == 5 )
                    {
                        Console.WriteLine("Yep, we've got 5!");
                        Console.WriteLine("Need {} for multiple statements in an if");
                    }
                    else if( x == 10 )
                        Console.WriteLin("Ten");
                    else
                        Console.Writeline( "Something else");
                        
                    switch( num )
                    {
                        case 1 : 
                            num += 1;
                        case 2 :
                            num = num * 2;
                        default:
                            num = 0;
                    }
                      
                
Loops
                //Pre-test
                while( i < 5 )
                    i++
                    
                for (int i = 0; i < 5; i++)
                {
                    int num = i * 2;
                    Console.WriteLine( "i*2=" + num);
                }
                
                //Post-test
                do
                    i++;
                while(i < 5 );
                
                //Collection iteration
                int[] numbers = {1, 2, 3, 4, 5}
                foreach( int num in numbers)
                    Console.WriteLine(num);
                    
                Loop control
                for( int i = 0; i <= 20; i++ )
                {
                    if( (i % 2) == 0 )
                        continue;       //Skip to next iteration
                    else if( (i%3) == 0 )
                        break;          //Exit loop
                }
                
Casting & Conversion
                    float floatNum = 9.9;
                    int intNum = (int) floatNum;    //decimal is truncated, intNum is set to 9
                    
                    string stringNum = "100";
                    int num = System.Convert.ToInt32(stringNum);    //System.Convert for things 
                                                                    //that can't be casted
                
Enumeration
                    public enum MethodType{ OVERWRITE, APPEND, PREPEND };
                    public enum CarType{ SUV=0, PICKUP=1; CAR=10};
                    
                    MethodType type = MethodType.OVERWRITE;
                    int typeNum = (int) MethodType.OVERWRITE;
                
Namespaces
                    namespace CodeHaven.CheatSheet.Utils
                    {
                        ...
                    }
                    
                    Or
                    
                    namespace CodeHaven
                    {
                        namespace CheatSheet
                        {
                            namespace Utils
                            {
                                ...
                            }
                        }
                    }
                
Classes & Structs
                    //Accessibility
                    public
                    protected
                    private
                    internal
                    protected internal
                    static
                    
                    //Inheritance
                    public class Base
                    {
                    }
                    
                    public class Child : Base
                    {
                    }
                    
                    //Structs are value type, but are similar to classes classes
                    //Except that they don't have default constructors, and can't be inherited
                    public struct Complex
                    {
                    }
                
Interfaces
                    //Interface definition
                    interface Vehicle
                    {
                        //Automatically public, no access keyword allowed
                        void Accelerate( int newSpeed );
                    }
                    
                    class Car : Vehicle
                    {
                        //Must be public to implement interface
                        public void Accelerate( int newSpeed );
                    }
                
Functions
                    //Arguments are passed by value, reference, output only reference respectively
                    public void DoSomething( int x, ref int y, out int z )
                    {
                        //DoStuff
                    }
                    
                    //Variable number of parameters
                    public int Sum( params int[] nums )
                    {
                        int result;
                        
                        for( int i = 0; i < nums.Length; i++
                            result += num[0];
                            
                        return result;
                    }
                    
                    int total = Sum( 10, 20, 5, 6);         //41
                
Exception Handling
                    //Throwing an exception
                    throw new Exception("Exception message.");
                    
                    //Catching an exception
                    try
                    {
                        y=0;
                        m=x/y;
                    }
                    catch( Exception e)
                    {
                        //Handle the exception here
                    }
                    finally
                    {
                        //This is always executed, whether exception is caught or not
                    }
                
Constructors/Desctructors
                    class Car
                    {
                        private int mSpeed;
                        private string mType;

                        //Constructors
                        public Car( int speed ) : this("Sedan", 0){}
                        public Car(string type, int speed )
                        {
                            mType = type;
                            mSpeed = speed;
                        }
                        
                        //Destructor   
                        public ~Car()
                        {   
                            //Used to release unmanaged resources.  Called when object
                            //is garbage collected.
                            
                        }
                    }
                
Properties
                    private int mSize;
                    
                    //get and set are optional
                    //leave get out for write only
                    //leave set out for read only
                    
                    public int size
                    {
                        get{ return mSize; }
                        set{ mSize = (value > 0) ? value : 0;}
                    }
                
Delegates
                    //declaring
                    delegate void GasPedalHandler(int newSpeed);
                    event GasPedalHandler btnEvent;
                    
                    //using
                    btnEvent += new ButtonHandler(Car.Accelerate);
                    btnEvent( 10 );
                    btnEvent -= new ButtonHandler(Car.Accelerate);