Show Menu
Cheatography

Just some stuff to help me remember

String methods

Length
Returns the number of characters in a string.
IndexOf
Finds the index of the first occurrence of a specified character or substring.
Substring
Retrieves a portion of the string based on a specified index or length.
ToUpper and ToLower
Converts a string to uppercase or lowercase, respec­tively
Trim
Removes leading and trailing whitespace characters from a string.
Replace
Replaces all occurr­ences of a specified character or substring with another.
Split
Splits a string into an array of substrings based on a specified delimiter.
Concat
Concat­enates multiple strings into a single string.
Format
Formats a composite string by replacing placeh­olders with corres­ponding values.
Compare
Compares two strings and returns an integer indicating their relative order.
Contains
Determines whether a specified character or substring exists within a string.
StartsWith and EndsWith
Checks if a string starts or ends with a specified character or substring.
PadLeft and PadRight
Adds padding characters to the left or right of a string to achieve a specified length.
IsNull­OrEmpty and IsNull­OrW­hit­eSpace
Checks if a string is null, empty, or consists only of whitespace charac­ters.
Join
oncate­nates an array of strings into a single string, using a specified delimiter.
ToChar­Array
Converts a string to an array of charac­ters.
CompareTo
Compares two strings and returns an integer indicating their relative order.
Substring (overload with start index and length)
Retrieves a substring from a string, starting at a specified index and with a specified length.
Remove
Deletes a specified number of characters from a string, starting at a specified position.
Equals
Compares two strings for equality, taking into account cultur­e-s­pecific or case-s­ens­itive compar­isons.

Logical And Arithmetic Operators

&&
AND
||
OR
+
Add
-
Subtract
*
Multiply
/
Divide
%
Modulo
++
Increase 1
--
Decrease 1

Arrays

string[] planets =
            {
               "Mercury", "Venus", "Mars",
               "Earth", "Jupiter", "Saturn", 
               "Uranus", "Neptune", "Pluto"
            };

            for (int i = 0; i < planet­s.L­ength; i++)
            {
                Console.WriteLine(planets[i]);
            }

            foreach (string planet in planets)
            {
                Console.WriteLine(planet);
            }

-----------------------------------

array sort & reverse:
string[] names = { "Jane", "Frank", "Alice", "Tom" };

            Array.S­or­t(names);
            foreach (string name in names)
            {
                Consol­e.W­rite(name + " ");
            }

Array.R­ev­ers­e(names);
            foreach (string name in names)
            {
                Consol­e.W­rite(name + " ");
            }

Switch case

switch(expression) {
   case constant-expression1  :
      statement(s);
      break;
   case constant-expression2  :
   case constant-expression3  :
      statement(s);
      break;
  
   / you can have any number of case statements /
   default : / Optional /
   statement(s);
}

While loop

while(­con­dition)
{
statem­ent(s);
}

Here, statem­ent(s) may be a single statement or a block of statem­ents. The condition may be any expres­sion, and true is any non-zero value. The loop iterates while the condition is true.

When the condition becomes false, program control passes to the line immedi­ately following the loop.

do while loop

do
{
statem­ent(s);
} while( condition );

Notice that the condit­ional expression appears at the end of the loop, so the statem­ent(s) in the loop execute once before the condition is tested.

If the condition is true, the flow of control jumps back up to do, and the statem­ent(s) in the loop execute again. This process repeats until the given condition becomes false.

Classes and objects

--Classes--
class Car 
{
string color = "red";
}

--Object--
class Car 
{
  string color = "red";

  static void Main(string[] args)
  {
    Car myObj = new Car();
    Console.WriteLine(myObj.color);
  }
}

--Multiple Objects--
class Car
{
  string color = "red";
  static void Main(string[] args)
  {
    Car myObj1 = new Car();
    Car myObj2 = new Car();
    Console.WriteLine(myObj1.color);
    Console.WriteLine(myObj2.color);
  }
}

Comparison Operators

<
Less than
>
Greater than
<=
Less than or equal to
>=
Greater than or equal to
==
Equal to
!=
Not equal to
 

Comments

// Single line
/* Multiple

lines */
/// XML comments on single line

Assignment

=
Assignment
+=
Add
-=
Subtract

Lists

List<int> number = new()
            {
                4, 6, 8, 1, 10, 2, 7, 3, 9, 5
            };

            number.Add(12);
            number.Add(11);
            number.Remove(1);
            number.Sort();

            foreach (int num in number)
            {
                Console.WriteLine(num);
            }

---------------------------------
            
            List<string> word = new()
            {
                "Delta", "Charlie", "Bravo", "Foxtrot", 
                "Echo", "Alpha", "Golf"
            };

            word.Add("India");
            word.Add("Hotel");
            word.Remove("Alpha");
            word.Sort();

            foreach (string phonetic in word)
            {
                Console.WriteLine(phonetic);
            }

Casting

Implicit Casting (autom­ati­cally)
converting a smaller type to a larger type size char -> int -> long -> float -> double.
Explicit Casting (manually)
converting a larger type to a smaller size type double -> float -> long -> int -> char.
Implicit Casting:
int myInt = 9;
double myDouble = myInt; // Automatic casting: int to double

Consol­e.W­rit­eLi­ne(­myInt); // Outputs 9
Consol­e.W­rit­eLi­ne(­myD­ouble); // Outputs 9

Explicit Casting:
double myDouble = 9.78;
int myInt = (int) myDouble; // Manual casting: double to int

Consol­e.W­rit­eLi­ne(­myD­ouble); // Outputs 9.78
Consol­e.W­rit­eLi­ne(­myInt); // Outputs 9

for loop

for ( init; condition; increment )
{
statem­ent(s);
}

The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.

Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the for loop.

After the body of the for loop executes, the flow of control jumps back up to the increment statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.

The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again testing for a condit­ion). After the condition becomes false, the for loop termin­ates.

Data types

bool
Represents a Boolean value, either true or false.
char
Represents a single Unicode character.
string
Represents a sequence of charac­ters.
int
Represents signed integers (whole numbers) within the range -2,147­,48­3,648 to 2,147,­483­,647.
double
Represents double­-pr­ecision floati­ng-­point numbers with decimal values. Sufficient for storing 15 decimal digits
decimal
Represents decimal numbers with high precision and a larger range.
float
Represents single­-pr­ecision floati­ng-­point numbers with decimal values. Sufficient for storing 6 to 7 decimal digits
long
Represents signed integers with a larger range than int, from -9,223­,37­2,0­36,­854­,77­5,808 to 9,223,­372­,03­6,8­54,­775­,807.
DateTime
Represents dates and times.

Example method

namespace MyApplication
{
  class Program
  {
    static void Main(string[] args)
    {
      MyMethod();
    }

    static void MyMethod()
    {
      Console.WriteLine("I have a red banana");
    }
  }
}

Ternary operator

condition ? consequent : altern­ative

string GetWea­the­rDi­spl­ay(­double tempIn­Cel­sius) => tempIn­Celsius < 20.0 ? "­Col­d." : "­Per­fec­t!";

Consol­e.W­rit­eLi­ne(­Get­Wea­the­rDi­spl­ay(­15)); // output: Cold.
Consol­e.W­rit­eLi­ne(­Get­Wea­the­rDi­spl­ay(­27)); // output: Perfect!
 

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