C # Basics revisited
C# Basics
_____________________________________________________________
Variables
______________________________________________________________
Variables: Named value
stored in memory – (case sensitive in C#)
· Two
types:
o
Value Types
§ Stores
their values inside their own memory locations
§ Hold
values / assigning one value type to another (literally copies values)
§ [BENS]-Bool,
Enum, Numeric types, Struct (STACK storage)
o
Reference Types:
§ contains
only address to the dynamic memory location where the value is stored.
§ (HEAP storage) - Objects that Stores
references to the actual data
§ [CODIS] - Classes, Objects, Delegates,
Interfaces Strings
Sample: employeeName
C# Reserved Variables – Keywords
e.g. byte, int, async
Declaring
Variables
static void Main(string[] args)
{
//vars
int a;
double change, temperature;
// Creating and initializing list
List<int> lst = new List<int>(){2,4,6};
//Assign Values to Vars
int x = 21;
int y = x + 7;
bool student = false;
a = 5;
temperature = 72.5;
System.Console.WriteLine("in list:" + lst.Count);
}
Operators
Category |
Operators |
Arithmetic |
+, -, *, / |
Relational |
== != < >= > >= |
Logical |
! && || |
Increment
and Decrement Operators ( -- ++)
{
int a = 15;
int b = ++a;
int c = 20;
int d = c++;
System.Console.WriteLine($"a = {a} b = {b} c={c} d={d:N2}");
//RESULT: a = 16 b = 16 c=21 d=20.00
a=0;b=0;c=0;d=0;
a = 15;
b = --a;
c =20;
d = c--;
System.Console.WriteLine($"a = {a} b = {b} c={c} d={d:N2}");
//RESULT: a = 14 b = 14 c=19 d=20.00
}
Type Conversion
Implicit
Conversion
int x = 100;
double y =12.55;
//x = x + y; //Error: cannot implicitly convert Double to Int)
y = x + y;
System.Console.WriteLine(y);
//Result:x now double -> 112.55
Explicit
Conversion
int a = 21;
int b =5;
double c = (double)a / b;
System.Console.WriteLine(c);
//Result: 4.2
Convert
Class
int r = 23;
string s1 = Convert.ToString(r); //or
string s2 = r.ToString(); //or
System.Console.WriteLine($"{s1} and {s2}");
//Result: 23 and 23
Input
and Output in C#
Console.WriteLine("Enter 1st number");
double num1 = Convert.ToDouble(System.Console.ReadLine());
Console.WriteLine("Enter 2nd number");
double num2 = Convert.ToDouble(System.Console.ReadLine());
var Result = num1 + num2;
System.Console.WriteLine($"{num1} + {num2} = {Result:F2}");
Strings
Substring, IndexOf, LastIndexOf
string aString = "A basic to use in example for manipulation";
string sectionWithoutLength = aString.Substring(10);
string sectionWithLength = aString.Substring(5, 10);
Console.WriteLine(sectionWithoutLength);// use in example for manipulation
Console.WriteLine(sectionWithLength);// ic to use
int charPosition = aString.IndexOf('i'); //5
int stringPosition = aString.IndexOf("use");//11
int charPosWithStartIndex = aString.IndexOf('s', 10);//12
int stringPosWithStartIndex = aString.IndexOf("use", 10);//11
int lastPosition = aString.LastIndexOf('o');//40
int stringLastPosition = aString.LastIndexOf("for");//26
Contains, StartsWith, EndsWith
bool containsResult = aString.Contains("use");//True
bool startsWithResult = aString.StartsWith("bad");//False
bool endsWithResult = aString.EndsWith("manipulation");//Treu
ToLower, ToUpper
string strUp = aString.ToUpper();
string strLo = aString.ToLower();
Conditions
Basic Condition Statements
if (condition)
{
< expression2 > ;
}
else
{
< expression3 > ;
}
if (condition)
{
< expression1 > ;
}
else if (condition2)
{
< expression3 > ;
}
else
{
< expression4> ;
}
Nested if
if (condition)
{
if (condition2)
{
< expression1 > ;
}
else
{
< expression2 > ;
}
}
else if (condition2)
{
< expression3 > ;
}
else
{
< expression4 > ;
}
Comments
Post a Comment