See Sharp Cheat Sheet
by Kemmojoo via cheatography.com/28261/cs/8310/
Comments Loops (cont) Objects Delegates / Events (cont)
// 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#
By Kemmojoo Published 31st May, 2016. Sponsored by ApolloPad.com
cheatography.com/kemmojoo/ Last updated 31st May, 2016. Everyone has a novel in them. Finish Yours!
Page 1 of 4. https://apollopad.com
See Sharp Cheat Sheet
by Kemmojoo via cheatography.com/28261/cs/8310/
Program Structure Functions Functions (cont) Structs (cont)
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
By Kemmojoo Published 31st May, 2016. Sponsored by ApolloPad.com
cheatography.com/kemmojoo/ Last updated 31st May, 2016. Everyone has a novel in them. Finish Yours!
Page 2 of 4. https://apollopad.com
See Sharp Cheat Sheet
by Kemmojoo via cheatography.com/28261/cs/8310/
Console I/O (cont) Arrays (cont) Classes / Interfaces (cont) File I/O (cont)
System.Console.WriteLine(c string[] names = new class Articles: Authors { ("c:\\myfile.txt");
); //Prints 65 if user string[5]; ... string line =
enters "A" names[0] = "Steven"; } reader.ReadLine();
// Throws while (line != null) {
Data Types System.IndexOutOfRangeExce using System; Console.WriteLine(line);
ption line =
//Value Types
names[5] = "Sarah" interface IArticle{ reader.ReadLine();
bool
void Show(); }
byte, sbyte
// C# can't dynamically } reader.Close();
char (example: 'A')
resize an array.
short, ushort, int, uint,
//Just copy into new //Write out to binary file
long, ulong
array. class IAuthor:IArticle{ string str = "Text data";
float, double
string[] names2 = new public void Show() { int num = 123;
decimal
string[7]; System.Console.WriteLi BinaryWriter binWriter =
DateTime
// or ne("Show() method new
//Reference Types
names.CopyTo(names2, 0); Implemented"); BinaryWriter(File.OpenWrit
object
Array.Copy(names, names2, } e
string
names.Length); ("c:\\myfile.dat"));
int x;
binWriter.Write(str);
Console.WriteLine(x.GetTyp
float[,] twoD = new public static void binWriter.Write(num);
e())
float[rows, cols]; Main(string[] args) { binWriter.Close();
Console.WriteLine(typeof(i
twoD[2,0] = 4.5; IAuthor author = new
nt))
IAuthor(); //Read from binary file
int[][] jagged = new author.Show(); BinaryReader binReader =
//Type conversion
int[3][] { } new
float d = 3.5;
new int[5], new int[2], } BinaryReader(File.OpenRead
int i = (int) d
new int[3] }; ("c:\\myfile.dat"));
jagged[0][4] = 5; File I/O str =
Arrays
binReader.ReadString();
using System.IO;
int[] nums = {1, 2, 3};
Classes / Interfaces num =
//Write out to text file
for (int i = 0; i <
binReader.ReadInt32();
//Accessibility keywords StreamWriter writer =
nums.Length; i++)
binReader.Close();
public File.CreateText
Console.WriteLine(nums[i
private ("c:\\myfile.txt");
]);
internal writer.WriteLine("Out to
protected file.");
// 5 is the size of the
protected internal writer.Close();
array
static
//Read all lines from text
file
//Inheritance StreamReader reader =
File.OpenText
By Kemmojoo Published 31st May, 2016. Sponsored by ApolloPad.com
cheatography.com/kemmojoo/ Last updated 31st May, 2016. Everyone has a novel in them. Finish Yours!
Page 3 of 4. https://apollopad.com
See Sharp Cheat Sheet
by Kemmojoo via cheatography.com/28261/cs/8310/
Constants Exception Handling Constructors / Destructors (cont) Properties (cont)
const int MAX_AUTHORS = class Withfinally{ } }
25; public static void } }
readonly float Main() { int month;
MIN_RANKING = 5.00; try { Properties
int x = 5;
private int _size;
Choices int y = 0; public int Year{
int z = x/y; get {
greeting = age < 20 ? public int Size {
Console.WriteLine(z) return year;
"What's up?" : "Hello"; get {
; }
if (x != 100 && y < 5){ return _size;
} set {
// Multiple statements }
catch(DivideByZeroExceptio year = value;
must be enclosed in {} set {
n e) { }
x *= 5; if (value < 0)
System.Console.Write }
y *= 2; _size = 0;
Line("Error occurred"); int year;
} else
} finally {
if (x > 5) _size = value;
System.Console.Write
x *= y; }
Line("Thank you"); public bool
else if (x == 5) }
} IsLeapYear(int year) {
x += y;
} return year%4== 0
else if (x < 10) foo.Size++;
} ? true: false;
x -= y; using System;
}
else class Date{
Constructors / Destructors public void SetDate
x /= y; public int Day{
(int day, int month, int
//Must be integer or class TopAuthor { get {
year) {
string private int _topAuthor; return day;
this.day = day;
switch (color){ }
this.month =
case "black": public TopAuthor() { set {
month;
case "red": r++; _topAuthor = 0; day = value;
this.year = year;
break; } }
}
case "blue" }
}
break; public TopAuthor(int int day;
case "green": g++; topAuthor) {
break; this._topAuthor=
default: other++; topAuthor public int Month{
break; } get {
} return month;
~TopAuthor() { }
// Destructor code to set {
free unmanaged resources. month =
// Implicitly creates value;
a Finalize method
By Kemmojoo Published 31st May, 2016. Sponsored by ApolloPad.com
cheatography.com/kemmojoo/ Last updated 31st May, 2016. Everyone has a novel in them. Finish Yours!
Page 4 of 4. https://apollopad.com