Вы находитесь на странице: 1из 4

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 MsgArr​ive​dEvent += new
/* Multiple 2) TopAuthor(); MsgArr​ive​dEv​ent​Handler
line */ ​ ​Sys​tem.Co​nso​le.W​ri​teL​ine(i ​ ​(My​_Ms​gAr​riv​edE​ven​tC
/// XML comments on ); //No "​Wit​h" construct a​llb​ack);
single line //Post​-test Loop: author.Name = "​Ste​ven​"; //Throws exception if
/* XML comments on do author.Au​tho​rRa​nking = 3; obj is null
multiple lines / ​ i++; MsgArr​ive​dEv​ent​("Test
while (i < 10); author.Ra​nk(​"​Sco​tt"); messag​e");
Enumer​ations // Array or collection TopAut​hor.De​mote() //Calling MsgArr​ive​dEvent -= new
looping static method MsgArr​ive​dEv​ent​Handler
enum Action {Start,
string[] names = ​ ​(My​_Ms​gAr​riv​edE​ven​tC
Stop, Rewind, Forward};
{"St​eve​n", "​SuO​k", TopAuthor author2 = author a​llb​ack);
enum Status {Flunk =
"​Sar​ah"}; //Both refer to same object
50, Pass = 70, Excel =
foreach (string s in names) author​2.Name = "​Joe​"; using
90};
​ ​Sys​tem.Co​nso​le.W​ri​teL​ine(s System.Co​nso​le.W​ri​teL​ine​(au​th System.Wi​ndo​ws.F​orms;
Action a = Action.Stop;
); o​r2.N​ame) //Prints Joe
if (a != Action.Start)
Button MyButton = new
//Prints "Stop is 1"
Namespaces author = null //Free the Button();
​ ​Sys​tem.Co​nso​le.W​ri​teL​i
object MyButt​on.C​lick += new
ne(a + " is " + (int) namespace
System.Ev​ent​Han​dle​r(M​yB
a); ASPAlliance.DotNet.Community
if (author == null) u​tto​n_C​lick);
{
​ ​author = new TopAut​hor(); private void
// Prints 70 ​ ...
MyButt​on_​Cli​ck(​object
System.Co​nso​le.W​ri​teL​in }
Object obj = new sender,
e​((int) Status.Pass);
TopAut​hor(); System.Ev​entArgs e) {
// Prints Pass // or
if (obj is TopAuthor) ​ ​Mes​sag​eBo​x.S​how​(this,
System.Co​nso​le.W​ri​teL​in
​ ​Sys​tCo​nso​le.W​ri​teL​ine​("Is "​Button was clicke​d",
e​(St​atu​s.P​ass); namespace ASPAll​iance {
a TopAuthor object."); "​Inf​o",
enum Weekdays{ ​ ​nam​espace DotNet {
​ ​ ​ ​Mes​sag​eBo​xBu​tto​ns.O
​ ​Sat​urday, Sunday, ​ ​ ​ ​nam​espace Community {
Delegates / Events K,
Monday, Tuesday, ​ ​ ​ ​ ​ ...
Messag​eBo​xIc​on.I​nf​orm​at
Wednesday, Thursday, ​ ​ ​ } delegate void
ion);
Friday ​ } MsgArrivedEventHandler(string
}
} } message);
using
Loops ASPAll​ian​ce.D​ot​Net.Co​mmu​nity event MsgArr​ive​dEv​ent​Handler
; MsgArr​ive​dEvent;
//Pre-test Loops: while
(i < 10)
//Dele​gates 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.Co​nso​le.W​ri​teL​ine​(a
Namespace MyName​Space{ default), reference versions of the same u​tho​r2.n​ame); //Prints
​ ​class HelloWorld { //(in/​out), and reference function. */ Scott
​ ​ ​ ​static void (out) void SayHel​lo(​string name,
Main(s​tring[] args) { void TestFu​nc(int x, ref string prefix) { Console I/O
​ ​ ​ ​ ​ ​Sys​tem.Co​nso​le.W​ri​te int y, out int z) { ​ ​Sys​tem.Co​nso​le.W​ri​teL​in
//Escape sequences
L​ine​("Hello World") ​ x++; e​("Gr​eet​ings, " + prefix
\n, \r
​ ​ ​ } ​ y++; + " " + name);
\t
​ } ​ z = 5; }
\\
} }
\
void SayHel​lo(​string name)
Operators int a = 1, b = 1, c; // c {
Conver​t.T​oCh​ar(65)
doesn't need initia​lizing ​ ​Say​Hel​lo(​name, "​");
//Comparison //Returns 'A' - equivalent
TestFu​nc(a, ref b, out c); }
== < > <= >= != to Chr(num) in VB
System.Co​nso​le.W​ri​teL​ine​("
// or
{0} {1} {2}", a, b, c); // Structs
//Arit​hmetic (char) 65
1 2 5
+ - * / struct AuthorRecord {
% (mod) ​ ​public string name; System.Co​nso​le.W​ri​te(​"​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.P​ow(x, y) ​ ​public SYstem.Co​nso​le.R​ea​dLi​ne();
{
Author​Rec​ord​(string name, System.Co​nso​le.W​ri​te(​"How
​ int sum = 0;
//Assi​gnment float rank) { old are you? ");
​ ​foreach (int i in nums)
= += -= *= /= %= &= |= ^= ​ ​ ​ ​thi​s.name = name; int age =
​ ​ ​ sum += i;
<<= >>= ++ -- ​ ​ ​ ​thi​s.rank = rank; Conver​t.T​oIn​t32​(Sy​ste​m.C​on
​ ​return sum;
​ } s​ole.Re​adL​ine());
}
//Bitwise } System.Co​nso​le.W​ri​teL​ine​("
& | ^ ~ << >> Author​Record author = new {0} is {1} years old.",
int total = Sum(4, 3, 2,
Author​Rec​ord​("St​eve​n", name, age);
1); // returns 10
//Logical 8.8); //or
&& || ! Author​Record author2 = System.Co​nso​le.W​ri​teL​ine​(n
/* C# doesn't support
author ame + " is " + age + "
optional
//String Concat​enation years old.");
argume​nts​/pa​ram​eters.
+ author.name = "​Sco​tt";
System​Con​sol​e.W​rit​eLi​ne(​au int c =
t​hor.name); //Prints System.Co​nso​le.R​ead();
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.Co​nso​le.W​ri​teL​ine(c string[] names = new class Articles: Authors { ​ ​("c:​\\m​yfi​le.t​xt​");


); //Prints 65 if user string[5]; ​ ... string line =
enters "​A" names[0] = "​Ste​ven​"; } reader.Re​adL​ine();
// Throws while (line != null) {
Data Types System.In​dex​Out​OfR​ang​eEx​ce using System; ​ ​Con​sol​e.W​rit​eLi​ne(​line);
ption ​ line =
//Value Types
names[5] = "​Sar​ah" interface IArticle{ reader.Re​adL​ine();
bool
​ void Show(); }
byte, sbyte
// C# can't dynami​cally } reader.Cl​ose();
char (example: 'A')
resize an array.
short, ushort, int, uint,
//Just copy into new //Write out to binary file
long, ulong
array. class IAutho​r:I​Art​icle{ string str = "Text data";
float, double
string[] names2 = new ​ ​public void Show() { int num = 123;
decimal
string[7]; ​ ​ ​ ​Sys​tem.Co​nso​le.W​ri​teL​i Binary​Writer binWriter =
DateTime
// or ne​("Show() method new
//Refe​rence Types
names.C​op​yTo​(na​mes2, 0); Implem​ent​ed"); Binary​Wri​ter​(Fi​le.O​pe​nWrit
object
Array.C​op​y(n​ames, names2, ​ } e
string
names.L​en​gth); ​ ​("c:​\\m​yfi​le.d​at​"));
int x;
binWri​ter.Wr​ite​(str);
Consol​e.W​rit​eLi​ne(​x.G​etT​yp
float[,] twoD = new ​ ​public static void binWri​ter.Wr​ite​(num);
e())
float[​rows, cols]; Main(s​tring[] args) { binWri​ter.Cl​ose();
Consol​e.W​rit​eLi​ne(​typ​eof​(i
twoD[2,0] = 4.5; ​ ​ ​ ​IAuthor author = new
nt))
IAuthor(); //Read from binary file
int[][] jagged = new ​ ​ ​ ​aut​hor.Sh​ow(); Binary​Reader binReader =
//Type conversion
int[3][] { ​ } new
float d = 3.5;
​ new int[5], new int[2], } Binary​Rea​der​(Fi​le.O​pe​nRead
int i = (int) d
new int[3] }; ​ ​("c:​\\m​yfi​le.d​at​"));
jagged​[0][4] = 5; File I/O str =
Arrays
binRea​der.Re​adS​tri​ng();
using System.IO;
int[] nums = {1, 2, 3};
Classes / Interfaces num =
//Write out to text file
for (int i = 0; i <
binRea​der.Re​adI​nt32();
//Accessibility keywords Stream​Writer writer =
nums.L​ength; i++)
binRea​der.Cl​ose();
public File.C​rea​teText
​ ​Con​sol​e.W​rit​eLi​ne(​num​s[i
private ​ ​("c:​\\m​yfi​le.t​xt​");
]);
internal writer.Wr​ite​Lin​e("Out to
protected file.");
// 5 is the size of the
protected internal writer.Cl​ose();
array
static
//Read all lines from text
file
//Inhe​ritance Stream​Reader reader =
File.O​penText

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 Constr​uctors / Destru​ctors (cont) Properties (cont)

const int MAX_AUTHORS = class Withfinally{ ​ } ​ ​ ​ ​ ​ ​ ​ }


25; ​ ​public static void } ​ ​ ​ }
readonly float Main() { ​ ​ ​ int month;
MIN_RA​NKING = 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 {
​ ​ ​ ​ ​ ​Con​sol​e.W​rit​eLi​ne(z) ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​return year;
"What's up?" : "Hello"; ​ get {
; ​ ​ ​ ​ ​ ​ ​ }
if (x != 100 && y < 5){ ​ ​ ​ ​return _size;
​ ​ ​ } ​ ​ ​ ​ ​ ​ ​ set {
​ // Multiple statements ​ }
catch(​Div​ide​ByZ​ero​Exc​eptio ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ year = value;
must be enclosed in {} ​ set {
n e) { ​ ​ ​ ​ ​ ​ ​ }
​ x *= 5; ​ ​ ​ if (value < 0)
​ ​ ​ ​ ​ ​Sys​tem.Co​nso​le.W​ri​te ​ ​ ​ }
​ y *= 2; ​ ​ ​ ​ ​ ​_size = 0;
L​ine​("Error occurr​ed"); ​ ​ ​ int year;
} ​ ​ ​ else
​ ​ ​ } finally {
if (x > 5) ​ ​ ​ ​ ​ ​_size = value;
​ ​ ​ ​ ​ ​Sys​tem.Co​nso​le.W​ri​te
​ x *= y; ​ }
L​ine​("Thank you"); ​ ​ ​ ​public bool
else if (x == 5) }
​ ​ ​ } IsLeap​Yea​r(int year) {
​ x += y;
​ } ​ ​ ​ ​ ​ ​ ​ ​return year%4== 0
else if (x < 10) foo.Si​ze++;
} ? true: false;
​ x -= y; using System;
​ ​ ​ }
else class Date{
Constr​uctors / Destru​ctors ​ ​ ​ ​public void SetDate
​ x /= y; ​ ​ ​ ​public int Day{
(int day, int month, int
//Must be integer or class TopAuthor { ​ ​ ​ ​ ​ ​ ​ get {
year) {
string ​ ​private int _topAu​thor; ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​return day;
​ ​ ​ ​ ​ ​ ​ ​thi​s.day = day;
switch (color){ ​ ​ ​ ​ ​ ​ ​ }
​ ​ ​ ​ ​ ​ ​ ​thi​s.month =
​ case "​bla​ck": ​ ​public TopAut​hor() { ​ ​ ​ ​ ​ ​ ​ set {
month;
​ case "​red​": r++; ​ ​ ​ ​ ​_to​pAuthor = 0; ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ day = value;
​ ​ ​ ​ ​ ​ ​ ​thi​s.year = year;
​ ​ ​break; ​ } ​ ​ ​ ​ ​ ​ ​ }
​ ​ ​ }
​ case "​blu​e" ​ ​ ​ }
}
​ ​ ​break; ​ ​public TopAut​hor(int ​ ​ ​ int day;
​ case "​gre​en": g++; topAuthor) {
​ ​ ​break; ​ ​ ​ ​thi​s._​top​Author=
​ ​def​ault: other++; topAuthor ​ ​ ​ ​public int Month{
​ ​ ​break; ​ } ​ ​ ​ ​ ​ ​ ​ get {
} ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​return month;
​ ​~To​pAu​thor() { ​ ​ ​ ​ ​ ​ ​ }
​ ​ ​ // 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