C# Cheat Sheet
C# Cheat Sheet
C# Cheat Sheet
int - stores integers (whole numbers), without decimals, such as 123 or -123
double - stores floating point numbers, with decimals, such as 19.99 or -19.99
char - stores single characters, such as 'a' or 'B'. Char values are surrounded by
single quotes
string - stores text, such as "Hello World". String values are surrounded by double
quotes
bool - stores values with two states: true or false
KONSTANTA
Display variables:
Console.WriteLine(„Hello“ + name);
Spajanje/dodavanje varijabla
string c = a + b
Koristi se kad int nemože pohranit vrjednost (ako je prevelika), broj mora završavat s L
(sintaksa)
CASTING VARIJABLA
built in metode:
Convert.ToBoolean, Convert.ToDouble, Convert.ToString, Convert.ToInt32 (int)
and Convert.ToInt64 (long):
GET USER INPUT
Console.ReadLine()
Console.WriteLine("Enter username:");
// Create a string variable and get user input from the keyboard and store it in the
variable
// Print the value of the variable (userName), which will display the input value
S KONVERTIRANJEM
Math.Max(a, b);
Math.Min(a, b);
Math.Sqrt(64);
Math.Abs(-5,4);
STRING LENGTH
txt.Length
OTHER METHODS
SPAJANJE STRINGOVA
string c = a + b
ili
string c =string.Concat(a, b);
ili
Substring()
extracts the characters from a string, starting from the specified character position/index,
and returns a new string
IF STATEMENT
if (a == b)
Console.WriteLine(„a = b“);
else
TERNARY OPERATOR
shorthand if-else
Instead of writing:
Console.WriteLine("Good day.");
else
Console.WriteLine("Good evening.");
Write:
Console.WriteLine(result);
SWITCH
int day = 4;
switch (day)
case 1:
Console.WriteLine("Monday");
break;
case 2:
Console.WriteLine("Tuesday");
break;
case 3:
Console.WriteLine("Wednesday");
break;
case 4:
Console.WriteLine("Thursday");
break;
case 5:
Console.WriteLine("Friday");
break;
case 6:
Console.WriteLine("Saturday");
break;
case 7:
Console.WriteLine("Sunday");
break;
Ako želimo imati case gdje ništa od navedenog se ne desi koristi: „default“
int day = 4;
switch (day)
case 6:
Console.WriteLine("Today is Saturday.");
break;
case 7:
Console.WriteLine("Today is Sunday.");
break;
default:
break;
WHILE LOOP
int i = 0;
while (i < 5)
Console.WriteLine(i);
i++;
}
DO/WHILE LOOP
do
while (condition);
PRIMJER
int i = 0;
do
Console.WriteLine(i);
i++;
int i = 0;
do
Console.WriteLine(i);
i++;
FOR LOOP
Console.WriteLine(i);
ZA LISTE:
if (i == 4)
{
continue;
}
Console.WriteLine(i);
string[] cars;
Console.WriteLine(myNum[0]);
myNum[1] = 15
myNum.Length;
Console.WriteLine(cars[i]);
}
Foreach
Console.WriteLine(i);
Sort arrays
// Sort a string
Array.Sort(cars);
Console.WriteLine(i);
// Sort an int
Array.Sort(myNumbers);
Console.WriteLine(i);
}
System.Linq NAMESPACE
using System;
using System.Linq;
namespace MyApplication
class Program
{
{
}
METODE
class Program
{
// code to be executed
}
static means that the method belongs to the Program class and not an object of
the Program class. You will learn more about objects and how to access
methods through objects later in this tutorial.
void means that this method does not have a return value. You will learn more
about return values later in this chapter
ZOVI METODU
MyMethod();
MyMethod("Liam");
MyMethod("Jenny");
MyMethod("Anja");
}
// Liam Refsnes
// Jenny Refsnes
// Anja Refsnes
DEFAULT VALUE
Console.WriteLine(country);
return 5 + x;
Console.WriteLine(MyMethod(3));
}
// Outputs 8 (5 + 3)
METHOD OVERLOADING
int MyMethod(int x)
float MyMethod(float x)
return x + y;
return x + y;
OOP
Creating a class
class Car
Creating object
class Car
{
Console.WriteLine(myObj.color);
}
class Car
class Program
Console.WriteLine(myObj.color);
}
// The class
class MyClass
// Class members
string color = "red"; // field
{
}
class Car
string color;
int maxSpeed;
myObj.color = "red";
myObj.maxSpeed = 200;
Console.WriteLine(myObj.color);
Console.WriteLine(myObj.maxSpeed);
}
METODE
class Car
{
}
{
}
KONSTRUKTORI
class Car
public Car()
{
}
{
Car Ford = new Car(); // Create an object of the Car Class (this will call
the constructor)
}
// Outputs "Mustang"
CONSTRUCTOR PARAMETERS
class Car
{
public string model;
{
model = modelName;
}
{
Console.WriteLine(Ford.model);
}
// Outputs "Mustang"
ACCESS MODIFIERS
The public keyword is an access modifier, which is used to set the access
level/visibility for classes, fields, methods and properties.
PROPERTIES
To get private variabls outside of class we need to use properties: get, set
class Person
{
}
class Person
{
private string name; // field
{
}
class Program
{
myObj.Name = "Liam";
Console.WriteLine(myObj.Name);
}
OUTPUT: Liam
Automatic properties:
class Person
{ get; set; }
class Program
{
myObj.Name = "Liam";
Console.WriteLine(myObj.Name);
}
{
Console.WriteLine("Tuut, tuut!");
}
}
class Program
{
// Call the honk() method (From the Vehicle class) on the myCar object
myCar.honk();
// Display the value of the brand field (from the Vehicle class) and the value of the
modelName from the Car class
}
If you don't want other classes to inherit you can use „sealed“ keyword
...
}
POLYMORPHISM
For example, think of a base class called Animal that has a method called animalSound().
Derived classes of Animals could be Pigs, Cats, Dogs, Birds - And they also have their own
implementation of an animal sound (the pig oinks, and the cat meows, etc.):
{
}
{
}
{
}
}
Now we can create Pig and Dog objects and call the animalSound() method on both of them
OUTPUT će biti
C# daje način da maknemo taj override s „virutal“ keywordom unutar parent klase i
koristimo „override“ keyword za svaku metodu koju ne želimo da parent kalsa overridea
Data abstraction is the process of hiding certain details and showing only essential
information to the user.
Abstraction can be achieved with either abstract classes or interfaces (which you will learn
more about in the next chapter).
Abstract class: is a restricted class that cannot be used to create objects (to access it, it
must be inherited from another class).
Abstract method: can only be used in an abstract class, and it does not have a body. The
body is provided by the derived class (inherited from).
INTERFACES
Interface je „abstract class“ koji može samo imati apstraktne metode i propertie (s praznim
tjelima)
To get the integer value from an item, you must explicitly convert the item to
an int: