You are on page 1of 14

thecodingguys

C# CHEAT SHEET & NOTES


http://www.thecodingguys.net http://blog.thecodingugys.net

January 17

2013
2013 thecodingguys.net

CONTENTS
ABOUT LICENSE C# SYNTAX
Basic C# Syntax Comments

1 1 3
3 3

VARIABLES
Variable Naming Data Types

3
3 3

STRINGS IF STATEMENT
Syntax Logical Operator Operators

4 5
5 5 6

FOR ITERATIONS
Syntax

6
7

SWITCH STATEMENT
Rules

7
8

WHILE STATEMENT DATE & TIME


Syntax Formatting Dates

8 8
8 9

ARRAYS
Sorting Arrays

9
10

METHODS MANAGING EXCEPTIONS


Syntax

10 11
11

CLASSES
Syntax

12
12

This document is not a full tutorial just small cheat sheet and has the basics of C#. For a full tutorial and updates go to http://www.thecodingguys.net/tutorials/csharp/csharp-tutorial http://www.thecodingguys.net/downloads

This work is licensed under the creative commons Attribution-NonCommercial-NoDerivs 3.0 Unported You may not alter, transform, or build upon this work. You may not use this work for commercial purposes. You are free to copy, distribute and transmit the work

Basic C# Syntax
Code must be in code blocks which are represented with {} curly braces.

Comments
/**/ // /// Block Comment Single Line Comment XML Comment

Variables are storage locations like a box; you put things in take things out. Each variable must be given a name and a type <type> <name>;

Variable Naming
The first character of a variable must be a letter underscore or @ symbol Subsequent characters may be letters numbers underscores. Dont create variables with similar names. http://www.thecodingguys.net/tutorials/csharp/csharp-variables

Data Types
Data
Int

Description
Integer Whole numbers without decimals From -2,147,483,648 to 2,147,483,647

String Char Double Boolean

Set of characters must be in double quotes Single Characters Floating point numbers but more accurate. True or False

Strings are enclosed in double quotes A string is a data type

Escape character is \ New line starts with \n Concatenation starts is + or & http://www.thecodingguys.net/tutorials/csharp/csharp-strings

Operator \

Description Escape Character

Example Console.WriteLine("Asim Said: \"Welcome to thecodingguys\""); Console.WriteLine(@"Go to: C:\Windows\System32\CMD.e xe"); Console.WriteLine("Wow look a \n new line"); Console.WriteLine("What was that noise \a ?"); Console.WriteLine("{0:C}", 5.00);

Escapes the escape process New Line

\n

\a {0:C}

Sounds Beep Currency Symbol

{0:N} {0:P}

Separate numbers with dot or comma Percentage symbol

Console.WriteLine("{0:N}", 1000000); Console.WriteLine("{0:P}", 35);

Syntax
If (condition) { Code to execute }

If (condition) { Code to execute } else{ }

http://www.thecodingguys.net/tutorials/csharp/csharp-if-statement

If statements are used for making decisions.

Logical Operator
Operator
&& == != < And Equal to Not equal to Less than

Description

> <= >=

More than Less than but equal to More than but equal to

Operators
This is a list of C# operators, these are commonly used, you should learn all these operators.

Operator
+ * / < > <= >= && == || \n \a () . \ @

Description
Adding or Concatenation Subtraction Multiplication Division Less than More than Less than but equal to More than but equal to And Is equal to OR New Line (strings) Sound Beep Method invocation operator Member Access operator Escape character Skip escape sequence

http://www.thecodingguys.net/reference/csharp/csharp-operators-ref

For Iterations are good for repeating a process over

and over again (iteration means loop).

Syntax
for (int i = 0; i < length; i++) { } http://www.thecodingguys.net/tutorials/csharp/csharp-for-iterations

A switch statement is like an If Else statement going through code until something is true. Switch statements only evaluate one variable. switch (expression) { case constant-expression; break; default : statement break; }

Rules
The constant-expressions must be different Switch statement expression must be primitive data type (int, string, bool, char) if you want double or float you need to use if statement You always need to include the break keyword If you need to calculate your case you must use if statement http://www.thecodingguys.net/tutorials/csharp/csharp-switch-statement

The while iteration statement loops a piece of code. It evaluates something which is true. while (boolexpression) { Code to execute }

http://www.thecodingguys.net/tutorials/csharp/csharp-while-statement

Syntax
DateTime.Now;

Prints out the full date and time in the following format: 26/07/2012 18:07:49 Note: The format may be different depending on your system region settings. The settings above are for the UK format (DD/MM/YYY) and time is 24 hour format.

Formatting Dates
Method / Property Name
ToLongDateString ToShortDateString ToLongTimeString ToShortTimeString Now Day Year Month DayofWeek Hour Minute AddHours AddDays AddMonth AddYear

Description
Prints out the day in the following format 17 July 2012 Prints out the date is short format 17/7/2012 Gets the current time in the format HH:MM:SS (Hours, Minutes, Seconds) Gets the current time in the format HH:MM (Hours and Minutes) Gets the current date and time Gets the current day as Integer. (E.g. today is 27 July so it will get 27) Gets the year gets the month as Integer Gets the day. Gets the current hour Gets the current minute Adds hours to the current time Adds days to the current day Adds months to the current month Adds Year to the current year

http://www.thecodingguys.net/tutorials/csharp/csharp-dates-and-times

Arrays are like a bucket which can contain more than one variable. (A collection of variables) Int [] numbers = new int [5] Numbers [0] = 4; Numbers [1] = 5; Numbers [2] = 6; Numbers [3] = 7; Console.WriteLIne(numbers[1].Tostring()); Console.ReadLine(); [] = Index Access Operator Hold one Data Type Are accessed using Index Value.

Sorting Arrays
To sort arrays you use the method Array.Sort(array name) http://www.thecodingguys.net/tutorials/csharp/csharp-arrays

A method is a sequence of statements methods are declared within a class. Methods are useful if you need to use the same code over and over again. <Returntype> name (parameterList) { Body Code }

Private means a method can only be accessed in the same class, and public means it can be accessed outside of the class. If you do not want a return type use the keyword static

http://www.thecodingguys.net/tutorials/csharp/csharp-methods

There are always computer issues in programs and to catch the exception you use a try catch block.

Syntax
try{ } Catch (<exception name> <local variable>) { } EXAMPLE try { Console.WriteLine("Pick a number"); x = Console.ReadLine(); Console.WriteLine(int.Parse(x) + a);

} catch (FormatException fex) { Console.WriteLine(fex.Message); } Console.ReadLine(); http://www.thecodingguys.net/tutorials/csharp/csharp-managing-errors

Classes start with the class keyword Classes contain methods and events When you want to access a class you initialize the variable.

Syntax
class <class name> { } When you access the class you access it like this; car myCar = new car();

*Car is a class I made http://www.thecodingguys.net/tutorials/csharp/csharp-creating-andmanaging-classes

For updates and other tutorials, see: http://www.thecodingguys.net/downloads And http://www.thecodingguys.net

Follow me on:

IF YOU FOUND THIS USEFUL, PLEASE SHARE IT!

You might also like