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 < planets.Length; i++)
{
Console.WriteLine(planets[i]);
}
foreach (string planet in planets)
{
Console.WriteLine(planet);
}
-----------------------------------
array sort & reverse:
string[] names = { "Jane", "Frank", "Alice", "Tom" };
Array.Sort(names);
foreach (string name in names)
{
Console.Write(name + " ");
}
Array.Reverse(names);
foreach (string name in names)
{
Console.Write(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(condition)
{
statement(s);
}
Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, 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 immediately following the loop. |
do while loop
do
{
statement(s);
} while( condition );
Notice that the conditional expression appears at the end of the loop, so the statement(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 statement(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 (automatically) |
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
Console.WriteLine(myInt); // Outputs 9
Console.WriteLine(myDouble); // Outputs 9
Explicit Casting:
double myDouble = 9.78;
int myInt = (int) myDouble; // Manual casting: double to int
Console.WriteLine(myDouble); // Outputs 9.78
Console.WriteLine(myInt); // Outputs 9
for loop
for ( init; condition; increment )
{
statement(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 condition). After the condition becomes false, the for loop terminates. |
Data types
bool |
Represents a Boolean value, either true or false. |
char |
Represents a single Unicode character. |
string |
Represents a sequence of characters. |
int |
Represents signed integers (whole numbers) within the range -2,147,483,648 to 2,147,483,647. |
double |
Represents double-precision floating-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-precision floating-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,372,036,854,775,808 to 9,223,372,036,854,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 : alternative
string GetWeatherDisplay(double tempInCelsius) => tempInCelsius < 20.0 ? "Cold." : "Perfect!";
Console.WriteLine(GetWeatherDisplay(15)); // output: Cold.
Console.WriteLine(GetWeatherDisplay(27)); // output: Perfect! |
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets