| 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 |
| 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;
nums[3] = 5;
int[,] twoD = new int[rows,cols];
twoD[0,0] = 2;
int[][] jagged = new int[3][]{ new int[3]. new int[5], new int[3]};
jagged[1][4] = 5;
|
| Operators |
* / % + -
&& || ! & | ^
& | ^ ~ << >>
= += -= *= /= %= &= |= ^= <<= >>=
++ --
?:
|
| 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 |
while( i < 5 )
i++
for (int i = 0; i < 5; i++)
{
int num = i * 2;
Console.WriteLine( "i*2=" + num);
}
do
i++;
while(i < 5 );
int[] numbers = {1, 2, 3, 4, 5}
foreach( int num in numbers)
Console.WriteLine(num);
for( int i = 0; i <= 20; i++ )
{
if( (i % 2) == 0 )
continue;
else if( (i%3) == 0 )
break;
}
|
| Casting & Conversion |
float floatNum = 9.9;
int intNum = (int) floatNum;
string stringNum = "100";
int num = System.Convert.ToInt32(stringNum);
|
| 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
{
...
}
namespace CodeHaven
{
namespace CheatSheet
{
namespace Utils
{
...
}
}
}
|
| Classes & Structs |
public
protected
private
internal
protected internal
static
public class Base
{
}
public class Child : Base
{
}
public struct Complex
{
}
|
| Interfaces |
interface Vehicle
{
void Accelerate( int newSpeed );
}
class Car : Vehicle
{
public void Accelerate( int newSpeed );
}
|
| Functions |
public void DoSomething( int x, ref int y, out int z )
{
//DoStuff
}
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);
|
| Exception Handling |
throw new Exception("Exception message.");
try
{
y=0;
m=x/y;
}
catch( Exception e)
{
}
finally
{
}
|
| Constructors/Desctructors |
class Car
{
private int mSpeed;
private string mType;
public Car( int speed ) : this("Sedan", 0){}
public Car(string type, int speed )
{
mType = type;
mSpeed = speed;
}
public ~Car()
{
}
}
|
| Properties |
private int mSize;
public int size
{
get{ return mSize; }
set{ mSize = (value > 0) ? value : 0;}
}
|
| Delegates |
delegate void GasPedalHandler(int newSpeed);
event GasPedalHandler btnEvent;
btnEvent += new ButtonHandler(Car.Accelerate);
btnEvent( 10 );
btnEvent -= new ButtonHandler(Car.Accelerate);
|