Show Menu

C# Basics Cheat Sheet by

c-

Formatting

Currency
"­{0:­C}", num1 OR num1.T­oSt­rin­g("C­2")
Decimal
"­{0:­000­}", num1 // rounds to three decimal places
Percent
"­{0:­P2}.", 0.9550) // formats as 95.50 %
New Line
\n
Horizontal Tab
\t
Vertical Tab
\v
Quotations
\"te­xt­\"
Left Justified
Consol­e.W­rit­eLi­ne(­Str­ing.Fo­rma­t("{­0,10} || {1,10}­", name, age)); //adds 10 spaces after the string
Right Justified
Consol­e.W­rit­eLi­ne(­Str­ing.Fo­rma­t("{­0,-10} || {1,-10­}", name, age)); //adds 10 spaces before the string

Switches

switch (monthNum)
{
case 1:
Console.WriteLine("WINTER");
break;

default:
Console.WriteLine("???");
}

OR

switch ((month)monthNum)
{
case Month.JAN:
Console.WriteLine("WINTER");
break;

default:
Console.WriteLine("???");
}

First or Last Digits

First 3 Digits
cardNum / 10; // increase if necessary
Last Digit
cardNum % 10;

GUI Example

private void btnConvert_Click(object sender, EventArgs e)
        {
            double percent = Convert.ToDouble(txtPercent.Text);
            if ((percent < 0) || (percent > 100))
            {
                lblResult.Text = "Enter # between 0 and 100";
            }
            else
            {

                string letterGrade = "F";
                if (percent >= 60 && percent < 70)
                {
                    letterGrade = "D";
                }
                else if (percent >= 70 && percent < 80)
                {
                    letterGrade = "C";
                }
                else if (percent >= 80 && percent < 90)
                {
                    letterGrade = "B";
                }
                else if (percent >= 90 && percent <= 100)
                {
                    letterGrade = "A";
                };

                lblResult.Text = letterGrade;
            }
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            lblResult.Text = "";
        }
 

Shortcut Operators

Add and Assign
counter += 1; // Take the value of counter, add 1 to it, and assign the result to counter
Prefix Increment
++counter; // Adds 1 to the value of counter, but isn't used yet
Postfix Increment
counter++; // Adds 1 to the value of counter and is immedi­ately used

Editing Strings

Remove Letters
myStri­ng.R­em­ove(0, 1) // "­tes­t" becomes "­est­"
Lower Case
myStri­ng.T­oL­ower() // makes string all lower case
Upper Case
myStri­ng.T­oU­pper() // makes string all upper case

Enumer­ations

Add to first bracket set
enum Month {JAN, FEB, MAR,...}
Change what number enum starts with
enum Month {JAN = 1, FEB, MAR,...}
Displaying Result
"You chose month {0}", (Month­)mo­nth­Num);

GUI Example

private void btnConvert_Click(object sender, EventArgs e)
        {
            double percent = Convert.ToDouble(txtPercent.Text);
            if ((percent < 0) || (percent > 100))
            {
                lblResult.Text = "Enter # between 0 and 100";
            }
            else
            {

                string letterGrade = "F";
                if (percent >= 60 && percent < 70)
                {
                    letterGrade = "D";
                }
                else if (percent >= 70 && percent < 80)
                {
                    letterGrade = "C";
                }
                else if (percent >= 80 && percent < 90)
                {
                    letterGrade = "B";
                }
                else if (percent >= 90 && percent <= 100)
                {
                    letterGrade = "A";
                };

                lblResult.Text = letterGrade;
            }
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            lblResult.Text = "";
        }
 

Data Types

Constants
const int TIRES_­PER_CAR = 4;
Bool
bool isGood­Salary = salary > 40000; // salary must be greater than 4000 to be true
Char
char initial = 'D';

Functions

String Length
myStri­ng.L­ength
Substring
myStri­ng.S­ub­str­ing(0, 1) // will only return "­t" if string is "­tes­t"
Equal
name1.E­qu­als­(name2) // its value is true or false
Compare To
name1.C­om­par­eTo­(na­me2); // value is integer—0 when the compared strings are equal, a negative number if the first string is less, and a positive number if the second string (the one in parent­heses) is less.
Compare
Compar­e(n­ame1, name2); // same as Compare To but must include the following statement at the top: using static System.St­ring;

Write Only Odds

int num1;
for (num1 = 1; num1 < 100; num++)
{

if (num1 % 2 != 0)
{
Console.WriteLine(num1);
}

}

Math

Absolute Value
Math.A­bs(num2 - num1)
Square Root
Math.S­qrt­(num1)
Raise to Power
Math.P­ow(5, 2) // result will be 25

Looping Examples

int x = 1;
   
        // Exit when x becomes greater than 4
        while (x <= 4)
        {
            Console.WriteLine("this is a string");
   
            // Increment the value of x for
            // next iteration
            x++;

-----------------------------------------------
for (loop variable initialization ; testing condition;
                              increment / decrement)
{
    // statements to be executed
}
-------------------------------------------------
do
{
    statements..
}while (condition);

-------------------------------------------------
// for loop begins when x=1
        // and runs till x <=4
        for (int x = 1; x <= 4; x++)
            Console.WriteLine("this is a string");

True or False

(num 1 > 0 && num2 < 0) || (num2 > 0 && num1 < 0)

Random Number Generator

Random randomNumGen = new Random ();
int randomNumGen;
randomNumGen = randomNumGen.Next(1000, 10000);

Download the C# Basics Cheat Sheet

2 Pages
//media.cheatography.com/storage/thumb/ryann_c-basics.750.jpg

PDF (recommended)

Alternative Downloads

Share This Cheat Sheet!

 

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

          Numeric Formats Cheat Sheet
          C# & Unity MonoBehaviour Cheat Sheet