Lecture No. 05
Dated: 29-04-2025
using System;
class Booleans {
public static void Main() {
bool content = true;
bool nocontent = false;
Console.WriteLine("It is {0} that C# Station provides C# programming language content.", content);
Console.WriteLine("The statement above is not {0}.", nocontent);
}
}
Integer Types
Type |
Size (in bits) |
Range |
sbyte |
\(8\) |
\(-128\) to \(127\) |
byte |
\(8\) |
\(0\) to \(255\) |
short |
\(16\) |
\(-32768\) to \(32767\) |
ushort |
\(16\) |
\(0\) to \(65535\) |
int |
\(32\) |
\(-2147483648\) to \(2147483647\) |
uint |
\(32\) |
\(0\) to \(4294967295\) |
long |
\(64\) |
\(-9223372036854775808\) to \(9223372036854775807\) |
ulong |
\(64\) |
\(0\) to \(18446744073709551615\) |
char |
\(16\) |
\(0\) to \(65535\) |
Floating Types
Type |
Size (in bits) |
Precision |
Range |
float |
\(32\) |
\(7\) digits |
\(1.5 \times 10^{-45}\) to \(3.4 \times 10^{38}\) |
double |
\(64\) |
\(15\)-\(16\) digits |
\(5.0 \times 10^{-324}\) to \(1.7 \times 10^{308}\) |
decimal |
\(128\) |
\(28\)-\(29\) decimal places |
\(1.0 \times 10^{-28}\) to \(7.9 \times 10^{28}\) |
Escape Sequences
Char |
Meaning |
Value |
\' |
Single quote |
0x0027 |
\" |
Double quote |
0x0022 |
\\ |
Backslash |
0x005C |
\0 |
Null |
0x0000 |
\a |
Alert |
0x0007 |
\b |
Backspace |
0x0008 |
\f |
Form feed |
0x000C |
\n |
New line |
0x000A |
\r |
Carriage return |
0x000D |
\t |
Horizontal tab |
0x0009 |
\v |
Vertical tab |
0x000B |
Operators
Category(by precedence) |
Operator(s) |
Associativity |
Primary |
x.y , f(x) , a[x] , x++ , x-- , new , typeof , default , checked , unchecked , delegate |
left |
Unary |
+ , - , ! , ~ , ++x , --x , (T)x |
right |
Multiplicative |
* , / , % |
left |
Additive |
+ , - |
left |
Shift |
<< , >> |
left |
Relational |
< , > , <= , >= , is , as |
left |
Equality |
== , != |
right |
Logical AND |
& |
left |
Logical XOR |
^ |
left |
Logical OR |
\| |
left |
Conditional AND |
&& |
left |
Conditional OR |
\|\| |
left |
Null Coalescing |
?? |
left |
Ternary |
?: |
right |
Assignment |
= *= /= %= += -= <<= >>= &= ^= \|= |
right |
Arrays
using System;
class Array {
public static void Main() {
int[] myints = { 5, 10, 15 };
bool[][] mybools = new bool[2][];
mybools[0] = new bool[2];
mybools[1] = new bool[1];
double[,] mydoubles = new double[2, 2];
string[] mystrings = new string[3];
Console.WriteLine("myints[0]: {0}, myints[1]: {1}, myints[2]: {2}", myints[0], myints[1], myints[2]);
mybools[0][0] = true;
mybools[0][1] = false;
mybools[1][0] = true;
Console.WriteLine("mybools[0][0]: {0}, mybools[1][0]: {1}", mybools[0][0], mybools[1][0]);
mydoubles[0, 0] = 3.147;
mydoubles[0, 1] = 7.157;
mydoubles[1, 1] = 2.117;
mydoubles[1, 0] = 56.00138917;
Console.WriteLine("mydoubles[0, 0]: {0}, mydoubles[1, 0]: {1}", mydoubles[0, 0], mydoubles[1, 0]);
mystrings[0] = "Joe";
mystrings[1] = "Matt";
mystrings[2] = "Robert";
Console.WriteLine("mystrings[0]: {0}, mystrings[1]: {1}, mystrings[2]: {2}", mystrings[0], mystrings[1], mystrings[2]);
}
}
Loops
while
using System;
class whileloop {
public static void Main() {
int myint = 0;
while (myint < 10)
{
Console.Write("{0} ", myint);
myint++;
}
Console.WriteLine();
}
}
for
using System;
class forloop {
public static void Main() {
for (int i = 0; i < 20; i++)
{
if (i == 10)
break;
if (i % 2 == 0)
continue;
Console.Write("{0} ", i);
}
Console.WriteLine();
}
}
foreach
using System;
class foreachloop {
public static void Main() {
string[] names = { "Cheryl", "Joe", "Matt", "Robert" };
foreach (string person in names)
{
Console.WriteLine("{0} ", person);
}
}
}
Methods
Syntax
Attributes modifiers return_type method_name(parameters) {statements}
using System;
class onemethod {
public static void Main() {
string mychoice;
onemethod om = new onemethod();
mychoice = om.getchoice();
}
string getchoice() {
return "example";
}
}