I created a coding diary blog to share my progress with you. We started the first day with c# course.
10/11/2021 notes
Firstly, we started with variables and types of variables. Defining a variable is not rocket science. I'll show you how to define a varible and assign it a value; for instance, we specify the type and asign it a value.
int num = 10;
string helloWorld = "Hello World!";
double thisIsADouble = 42.42d;
Data types: char, string, decimal, float, double, sbyte, float…
Here is a short version of data types table:
Type | Ranger |
---|---|
int | -2,147,483,648 to 2,147,483,647 |
byte | 0 to 255 |
sbyte | -128 to 127 |
short | -32,768 to 32,767 |
double | -1.79769313486232E+308 to 1.79769313486232E+308 |
We find their max and min value using MaxValue and MinValue.
sbyte num = -45;
num = 45;
byte byteMaks = byte.MaxValue;
byte byteMin = byte.MinValue;
Console.WriteLine($"The largest value of the byte type: {byteMaks}");
Console.WriteLine($"The smallest value of the byte type: {byteMin}");
short shortSayiMaks = short.MaxValue;
short shortSayiMin = short.MinValue;
long longMaks = long.MaxValue;
long longMin = long.MinValue;
double doubleNum = double.MaxValue;
Console.WriteLine($"The largest value of the double type = {doubleNum}");
To learn the type of a variable use GetType() method; for instance, x.GetType().
var x= 55;
var y =34.23d;
Console.ReadLine() // get user inputs.
Reference Type
These reference types hold a reference to the location of the data. if you change the variable value, the latest value is your new variable value. String and string are not the same things. String: create String Object string: data type. When you define a value using var, it retains the first attributed value type.
Arithmetic Operators
+, -, *, /, %, ++, – </b>
int a, b, c, d, result;
a = 2;b = 3; c = 4; d = 6;
result = a+b*(c+d)-a;
Console.WriteLine($"Result:{result}")
Logical Operators
Logical Or Operator : ||
Logical And Operator : &&
Logical Not Operator :!
Bitwise Operators
They work on bits.The table below show you why num1 & num2;// => 0001
& | 0 | 1 |
---|---|---|
0 | 0 | 0 |
1 | 0 | 1 |
byte num1 = 5; //0101
byte num2 = 3; //0011
int control7 = num1 & num2;// => 0001
int control8 = num1 | num2;// => 0111
byte control9 = (byte)~num2;
Console.WriteLine(control7);
Console.WriteLine(control8);
Console.WriteLine(control9);
Type conversions
Here, we are converting a type to an other type. To give an example, we can convert an integer to a float.
int num = 305;
byte byteNum = (byte)num;
byte byteNum2 = Convert.ToByte(num)
11/11/2021
int intNum = 256;
byte byteNum = (byte)intNum; // takes the rightmost 8 bits
Console.writline($"byte number: {byteNum}");
To avoid loses during type casting, we used checked{}.
checked{
int intNum = 256;
byte byteNum = (byte)intNum;// takes the rightmost 8 bits
Console.writline($"byte number: {byteNum}");
unchecked{
int ıntNum2 = 256;
byte =byteNum = (byte)intNum2 // even we lose some bites, casting does not stop.
}
}
Type casting with Parse method
Here, we are parsing strings and convert them to an int, double and short.
int variable1 = int.Parse("365");
double variable2 = double.Parse("34");
short variable3 = short.Parse("4321");
Boxing and Unboxing
Object is the base class for all derived classes
Boxing
This is the process of converting a value type to an object.
int limit = 120;
object box = limit;
Boxing with type conversion
object box2 = (object)limit;
Unboxing
To unboxe an object, it should be boxed and object type should be the same as the taget type.
float floatNum = 94.5f;
Boxing
object obj = floatNum ;
Unboxing
floatNum = (float)obj;