C Sharp Cheat Sheet
C Sharp Cheat Sheet
// Single line for (i = 2; i < = 10; i += TopAuthor author = new MsgArrivedEvent += new
/* Multiple 2) TopAuthor(); MsgArrivedEventHandler
line */ System.Console.WriteLine(i (My_MsgArrivedEventC
/// XML comments on ); //No "With" construct allback);
single line //Post-test Loop: author.Name = "Steven"; //Throws exception if
/* XML comments on do author.AuthorRanking = 3; obj is null
multiple lines / i++; MsgArrivedEvent("Test
while (i < 10); author.Rank("Scott"); message");
Enumerations // Array or collection TopAuthor.Demote() //Calling MsgArrivedEvent -= new
looping static method MsgArrivedEventHandler
enum Action {Start,
string[] names = (My_MsgArrivedEventC
Stop, Rewind, Forward};
{"Steven", "SuOk", TopAuthor author2 = author allback);
enum Status {Flunk =
"Sarah"}; //Both refer to same object
50, Pass = 70, Excel =
foreach (string s in names) author2.Name = "Joe"; using
90};
System.Console.WriteLine(s System.Console.WriteLine(auth System.Windows.Forms;
Action a = Action.Stop;
); or2.Name) //Prints Joe
if (a != Action.Start)
Button MyButton = new
//Prints "Stop is 1"
Namespaces author = null //Free the Button();
System.Console.WriteLi
object MyButton.Click += new
ne(a + " is " + (int) namespace
System.EventHandler(MyB
a); ASPAlliance.DotNet.Community
if (author == null) utton_Click);
{
author = new TopAuthor(); private void
// Prints 70 ...
MyButton_Click(object
System.Console.WriteLin }
Object obj = new sender,
e((int) Status.Pass);
TopAuthor(); System.EventArgs e) {
// Prints Pass // or
if (obj is TopAuthor) MessageBox.Show(this,
System.Console.WriteLin
SystConsole.WriteLine("Is "Button was clicked",
e(Status.Pass); namespace ASPAlliance {
a TopAuthor object."); "Info",
enum Weekdays{ namespace DotNet {
MessageBoxButtons.O
Saturday, Sunday, namespace Community {
Delegates / Events K,
Monday, Tuesday, ...
MessageBoxIcon.Informat
Wednesday, Thursday, } delegate void
ion);
Friday } MsgArrivedEventHandler(string
}
} } message);
using
Loops ASPAlliance.DotNet.Community event MsgArrivedEventHandler
; MsgArrivedEvent;
//Pre-test Loops: while
(i < 10)
//Delegates must be used with
i++;
events in C#
using System // Pass by value (in, Just create two different System.Console.WriteLine(a
Namespace MyNameSpace{ default), reference versions of the same uthor2.name); //Prints
class HelloWorld { //(in/out), and reference function. */ Scott
static void (out) void SayHello(string name,
Main(string[] args) { void TestFunc(int x, ref string prefix) { Console I/O
System.Console.Write int y, out int z) { System.Console.WriteLin
//Escape sequences
Line("Hello World") x++; e("Greetings, " + prefix
\n, \r
} y++; + " " + name);
\t
} z = 5; }
\\
} }
\
void SayHello(string name)
Operators int a = 1, b = 1, c; // c {
Convert.ToChar(65)
doesn't need initializing SayHello(name, "");
//Comparison //Returns 'A' - equivalent
TestFunc(a, ref b, out c); }
== < > <= >= != to Chr(num) in VB
System.Console.WriteLine("
// or
{0} {1} {2}", a, b, c); // Structs
//Arithmetic (char) 65
1 2 5
+ - * / struct AuthorRecord {
% (mod) public string name; System.Console.Write("What
// Accept variable number
/ (integer division if public float rank; 's your name? ");
of arguments
both operands are ints) string name =
int Sum(params int[] nums)
Math.Pow(x, y) public SYstem.Console.ReadLine();
{
AuthorRecord(string name, System.Console.Write("How
int sum = 0;
//Assignment float rank) { old are you? ");
foreach (int i in nums)
= += -= *= /= %= &= |= ^= this.name = name; int age =
sum += i;
<<= >>= ++ -- this.rank = rank; Convert.ToInt32(System.Con
return sum;
} sole.ReadLine());
}
//Bitwise } System.Console.WriteLine("
& | ^ ~ << >> AuthorRecord author = new {0} is {1} years old.",
int total = Sum(4, 3, 2,
AuthorRecord("Steven", name, age);
1); // returns 10
//Logical 8.8); //or
&& || ! AuthorRecord author2 = System.Console.WriteLine(n
/* C# doesn't support
author ame + " is " + age + "
optional
//String Concatenation years old.");
arguments/parameters.
+ author.name = "Scott";
SystemConsole.WriteLine(au int c =
thor.name); //Prints System.Console.Read();
Steven //Read single char
Console I/O (cont) Arrays (cont) Classes / Interfaces (cont) File I/O (cont)