Show Menu
Cheatography

C# CheatSheet Cheat Sheet by

programming code, basics

Data Types

string
2 bytes/char
bool
char
2 bytes
byte
1 byte
short
2 bytes
int
4 bytes
long
8 bytes
float
4 bytes
double
8 bytes
decimal
16 bytes
Float: Can hold up to seven signif­icant digits of accuracy
Double: Can hold 15 or 16 signif­icant digits of accuracy
Decimal: Has a greater precision and a smaller range. Suitable for financial and monetary calcul­ations

Displaying Variable Values

using System;
public class DisplaySomeMoney
[
       public static void Main()
       {
              double someMoney = 39.45;
              Console.WriteLine(someMoney);
        }
[

Operators

+
Addition
-
Subtra­ction
*
Multip­lic­ation
/
Division
%
Remainder

Suffixes

Put an F after a number to make it a float
 ­float pocket­change = 4.87F;
Put a D after it to make it a double (default)
 ­float pocket­change = 4.87D;
Put an M (money) after it to make it a decimal
  float pocket­change = 4.87M;
Scientific notation
 ­Inc­ludes an E (for exponent)

Methods

Define it – takes 4 steps
 ­Dec­lar­ation (or method header, or signature)
 {
 Code (method body)
 ­ ­Return value;
 }

Call it (invoke it)

Method declar­ations

Method with no return value, and passes no parameters
public static void Displa­yHe­llo­Text()
 ­Dis­pla­yHe­llo­Text();

Method with no return value, and passes one parameter
public static void Displa­ySa­les­Tax­(double pAmt)
 ­Dis­pla­ySa­les­Tax­(12­43.00);

Method with no return value, and passes two parameters
public static void Displa­ySa­les­Tax­(double pAmt, double ptaxRate)
 ­Dis­pla­ySa­lesTax (1243.00, .09);

Method that returns a value, and passes two parameters
public static double Displa­ySa­les­Tax­(double pAmt, double ptaxRate)
 ­Amount = Displa­ySa­lesTax (1243.00, .09);
 ­return Answer;
(if no return, then void, else, return type, e.g. integer32, string, double, etc)
 

Convert Methods

ToBool­ean()
to an equivalent Boolean value
ToInt16()
to a 16-bit signed integer
ToByte()
to an 8-bit unsigned integer
ToInt32()
to a 32-bit signed integer
ToChar()
to a Unicode character
ToInt64()
to a 64-bit signed integer
ToDeci­mal()
to a decimal number
ToString()
to its equivalent String repres­ent­ation
ToDouble()
to a double­-pr­ecision floati­ng-­point number
ToUInt16()
to a 16-bit unsigned integer

Arrays Functions

Array.S­or­t(a­rra­yName)
Arranges array items in ascending order
Array.L­ength
Length of the array
Array.R­ev­ers­e(a­rra­yName)
Reverses the order of items in an array
Array.B­in­ary­Sea­rch­(Ar­ray­Name, value)
Finds a requested value in a sorted array
Do not use Binary­Sea­rch() under these circum­stances
If your array items are not arranged in ascending order
If your array holds duplicate values and you want to find all of them
If you want to find a range match rather than an exact match

Array

Assigning values to array elements
double[] sales = new double­[20];

sales[0] = 2100.00;

sales[1] = 3256.06;

Printing an element value
Consol­e.W­rit­eLi­ne(­sal­es[1]);


**for loop to search a Parallel Array
for (int i = 0; i < nameAr­ray.Le­ngth; i++)

{

 if (nameA­rray[i] == dwarfName)

 {

 do something

 }

}

Multid­ime­nsional Arrays

One-di­men­sional or single­-di­men­sional array
Picture as a column of values. Elements can be accessed using a single subscript
Multid­ime­nsional arrays
Require multiple subscripts to access the array elements
 

Condit­ional Operators

Condit­ional AND operator
-Deter­mines whether two expres­sions are both true
-Written as two ampersands (&­&)
-You must include a complete Boolean expression on each side of the operator
Written as
If (age >= 0 && age < 120)      


Condit­ional OR operator
Used when you want some action to occur even if only one of two conditions is true
Written as
if (a == 1 || b+c > 3)

Loops

if stataments
if (statement)
{
  do something
}

while loop
while (statement)
{
  do something
}//end loop

-Definite loop or counted loop
Loop for which the number of iterations is predet­ermined
-Indef­inite loop
Value of a loop control variable is not altered by arithm­etic, but instead, is altered by user input (ask user a question inside the loop, exit based on a particular answer)

do-while loop
do
{
  do something in the loop
}
while (statement)


for loop
When using a for statement, you can indicate in one place:
Starting value for the loop control variable
Test condition that controls loop entry
Expression that alters the loop control variable
for (statement)
do something

DateTime and TimeSpan

DateTi­me(yr, mo,day)
currdt.Ad­dDa­ys(1)
DateTi­me(yr, mo,day, hr, min, sec)
currdt.Su­btr­act­Days(1)
DateTi­me.P­ar­se(­"­1/1­/20­15")
TimeSp­an(­hrs­,mi­n,sec)
DateTi­me.Now
TimeSp­an.F­ro­mSe­con­ds(120)
               
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          C Reference Cheat Sheet
          C program Cheat Sheet

          More Cheat Sheets by pao361

          Python Cheat Sheet