© Rays Technologies 1
C# BASICS
www.sunilos.com
www.raystec.com
10/14/16
10/14/16 © Rays Technologies 2
C#
Object Oriented Programming Language
Basic unit of C# is Object
An Object is an instances of Class
Class defines structure/skeleton of an
Object
The basic building blocks of C# is a Class
so a C# program is nothing but a class
10/14/16 © Rays Technologies 3
C#
Class contains methods and variables
Variables contains values
Methods perform operations
A Class should have default method
‘Main’ that is entry point of an
application
10/14/16 © Rays Technologies 4
Program
Procedural Language
int i = 5 //global variable
void main(){
..
a();
}
void a(int k){
int j = 0; //local variable
..
}
10/14/16 © Rays Technologies 5
Procedural Language Library
Library
Program 1 Program 2
Program 3 Program 4
10/14/16 © Rays Technologies 6
Class
C# Language
int i = 5 //global variable
void main(){
..
a();
}
void a(int k){
int j = 0; //local variable
..
}
10/14/16 © Rays Technologies 7
C # Library
Namespace
Class 1 Class 2
Class 3 Class 4
10/14/16 © Rays Technologies 8
C# Application
ApplicationApplication
Napspace 1 Namespace 2
Namespace 3 Namspace 4
10/14/16 © Rays Technologies 9
C# Program is a Class
public class HelloCSharp{
…….
}
A class may contain multiple variables and
methods
A Class should have default ‘Main’ method
that is an entry point for an application
10/14/16 © Rays Technologies 10
My First Program - HelloCSharp
 using System;
 public class HelloCSharp {
o public static void Main() {
o String name = “Vijay”;
o Console.WriteLine(“Hello” + name);
o }
 }
 using, public, class, static, and void are keywords
 Keywords are always written in small letters
10/14/16 © Rays Technologies 11
Compile & Run
Compile
o csc HelloCSharp.cs
o It will generate HelloCSharp.exe
Run
o HelloCSharp
10/14/16 © Rays Technologies 12
Keywords
class – is used to define a class
public – Access modifier shows accessibility of a
class or variable or method to other C# classes.
There are 3 access modifiers public, protected and
private
static – Memory for the static variables are
assigned only once in their life. Non-static
variables are called instance variables
void – is a NULL return type of Main method
10/14/16 © Rays Technologies 13
Commands
Console.write()/Console.WriteLine()
command is used to write text at standard
output device
String is a data type
Two strings are concated by + operator
o “Hello” + name
10/14/16 © Rays Technologies 14
Main method
 The Main method is sometimes called the application's
entry point.
 Can be declared as
 public static void Main(){}
 OR
 public static void Main(string args[]){}
 OR
public static int Main()
 {… return errorCode}
 OR
 public static int Main(string args[])
 {… return errorCode}
© Rays Technologies
While
10/14/16 15
10/14/16 © Rays Technologies 16
Print Hello C# 5 times - while
 using System;
 public class HelloWhile {
 public static void Main(String[] args) {
o boolean isAlive = true;
o int round = 0;
o while (isAlive) {
o Console.WriteLine(“Basanti will dance");
o round++;
o If(round>500 )
 isAlive =false;
o }
 }}
© Rays Technologies
For 10 Rs 5 shots
How
Much?
Okey!!
10/14/16 17
© Rays Technologies
For Loop
using System;
public class HelloFor {
public static void Main() {
ofor (int hits = 1; hits <= 5; hits++) {
Console.WriteLine( “Shot Balloon");
o}
o}
}
10/14/16 18
10/14/16 © Rays Technologies 19
Print Hello C# 5 times – do-while
using System;
public class HelloDoWhile {
public static void Main() {
o int i = 0;
o do {
 Console.WriteLine ( i+ “ Hello C#");
 i++;
o } while (i < 5);
}
}
10/14/16 © Rays Technologies 20
Add.java
using System;
public class Add {
public static void Main(String[] args) {
oint a = 5;
oint b = 10;
oint sum = a + b;
oConsole.WriteLine ("Sum is " + sum);
}
}
10/14/16 © Rays Technologies 21
switch
 public static void Main() {
 String country = “India”;
 switch(country) {
 case “India” :
o Console.WriteLine(“Language is Hindi");
o break;
 case “US” :
 case “England” :
o Console.WriteLine(“Language is English”);
o break;
 default :
o Console.WriteLine(“Don’t know language {0}”, country);
o break;
 }//switch
 }//method
10/14/16 © Rays Technologies 22
Value Types
int
long
sbyte
short
float
double
1
2
4
8
4
8
-128, +127
-9.223E18, +9.223E18
-32768, +32767
-2147483648, +2147483647
+3.4 E+38
+1.7 E+308
TypeType Size ByteSize Byte RangeRange
char 2 0, 65535 (Unicode)
bool 1 true, false
0
0
0
0
0
0
0
false
DefaultDefault
10/14/16 © Rays Technologies 23
Value Types
uint
ulong
byte
ushort
decimal
1
2
4
8
16
0, 255
0 .. 264-1
0, 65535
0,4294967295
± 1E-28 .. ±7.9E28
TypeType Size ByteSize Byte RangeRange
0
0
0
0
0
DefaultDefault
10/14/16 © Rays Technologies 24
Other Data Types
Reference types (composite)
o objects
o arrays
strings is a reference data type
10/14/16 © Rays Technologies 25
System.String class
 public static void Main() {
 String name = "Vijay Dinanth Chouhan";
 Console.WriteLine(" String Length- " + name.Length);
 Console.WriteLine(" 7 ths caharcter is- " + name[6]);
 Console.WriteLine(" Dina index is- " + name.IndexOf("Dina"));
 Console.WriteLine(" First i Position- " + name.IndexOf("i"));
 Console.WriteLine(" Last i Position- " + name.LastIndexOf("i"));
 Console.WriteLine(" a is replaced by b- " + name.Replace("a",
"b"));
 Console.WriteLine(" Chota vijay- " + name.ToLower());
 Console.WriteLine(" Bada vijay- " + name.ToUpper());
 Console.WriteLine(" Starts With Vijay- " +
name.StartsWith("Vijay"));
 Console.WriteLine(" Ends with han- " + name.EndsWith("han"));
 Console.WriteLine(" Substring- " + name.Substring(6));
 }
10/14/16 © Rays Technologies 26
System.Math class
 public static void main(String[] args) {
 Console.WriteLine("Math functions");
 Console.WriteLine(" Max 2,5 - " + Math.Max(2,5));
 Console.WriteLine(" Min 2,5 - " + Math.Min(2,5));
 Console.WriteLine(" Absolute 3.7 - " + Math.Abs(3.7));
 Console.WriteLine(" Exp 10 - " + Math.Exp(10));
 Console.WriteLine(" Square Root- " + Math.Sqrt(4));
 }
10/14/16 © Rays Technologies 27
Static vs Instance
String name = “Vijay”;
String surname = “Chohan”
C.WL(name.Length);
C.WL(surname.Length);
String.Length
C.WL(Math.Max(2,5));
C.WL(Math.Max(5,10));
10/14/16 © Rays Technologies 28
Hello <Name>
public class HelloName {
public static void Main(String[] args) {
 Console.WriteLine("Hello " + args[0]);
}
}
10/14/16 © Rays Technologies 29
Hello Name – if <condition>
 public class HelloName{
 public static void Main(String[] args) {
o if (args.Length == 1) {
 Console.WriteLine("Hello " + args[0]);
o } else {
 Console.WriteLine("Usage : HelloName <name>");
o }
 }
 }
10/14/16 © Rays Technologies 30
Hello All
public class HelloAll {
public static void Main(String[] args) {
o for (int i = 0; i < args.Length; i++) {
 Console.WriteLine(i + " = Hello " + args[i]);
o }
}
}
10/14/16 © Rays Technologies 31
Hello All - define Method
 public static void Main(String[] args) {
o PrintAll(args);
 }// main
 public static void PrintAll(String[] args) {
o int size = args.Length;
o for (int i = 0; i < size; i++) {
 Console.WriteLine((i + 1) + " = Hello " + args[i]);
o }
 }//myMethod
10/14/16 © Rays Technologies 32
Add.java
public class Add {
public static void Main(String[] args) {
o int a = Integer32.Parse(args[0]);
o int b = Integer32.Parse (args[1]);
o int sum = a + b;
o Console.WriteLine("Sum is " + sum);
}
}
10/14/16 © Rays Technologies 33
Division
public class Division {
public static void Main(String[] args) {
o int a = Integer32.Parse (args[0]);
o int b = Integer32.Parse (args[1]);
o double div = a/b;
o Console.WriteLine("Division is " + div);
}
}
10/14/16 © Rays Technologies 34
Return a Value
public class Division3 {
public static void Main(String[] args) {
o int a = Integer.Parse (args[0]);
o int b = Integer.Parse (args[1]);
o double div = getDivision(a, b);
o Console.WriteLine("Division is " + div);
}
 public static double getDivision(int a, int b) {
o double div = a / b;
o return div;
 }
 }
10/14/16 © Rays Technologies 35
10
One Dimension Array
20
[0]
18
..
10
8
6
4
2
[1]
[8]
[9]
[2]
[3]
[4]
[n]
Length
int[] table = new int[10];
int a = table[4];
int a = table[2];
int size = table.Length
10/14/16 © Rays Technologies 36
10
Initialize an Array
20
[0]
18
..
10
8
6
4
2
[1]
[8]
[9]
[2]
[3]
[4]
[n]
Length
int[] table = new int[10];
table[0] =2;
table[1] =4;
….
Or
int[] table =
{2,4,6,8,10,12,14,16,18,
20};
10/14/16 © Rays Technologies 37
Other Data Type Arrays
char[] chList = new char[5];
chList[0] = ‘A’….
Or
char[] chList = {‘A’,’B’,’C’,’D’,’E’}
String[] strList = new String[5];
strList[0] = “A”
strList[1] = “Bee”
Console.WriteLine(strList[0]);
….
Or
String[] strList = {“A”,”Bee”,”Cee”,”Dee”,”E”}
10/14/16 © Rays Technologies 3810length
2D Array
[0]
20
18
..
10
8
6
4
2
[1]
[8]
[9]
[2]
[3]
[4]
[n]
30
27
..
15
12
9
6
3
40
36
..
20
16
12
8
4
90
81
..
45
36
27
18
9
100
90
..
50
40
30
20
10
…
[0] [1] [2] [7] [8]
9
9
..
9
9
9
9
9
10/14/16 © Rays Technologies 39
 int[][] table = new int[10][9];
 table[1][5] = 5;
 int size = table.Length;
 int size = table[0].Length;
 int[][] rows = new int[10][];
 rows[0] = new int[9];
 rows[1] = new int[19];
 rows[2] = new int[29];
 int xyz = new int[10][9][2];
10/14/16 © Rays Technologies 40
Rectangular Array
10/14/16 © Rays Technologies 41
3D Array
20
[0]
18
..
10
8
6
4
2
[1]
[8]
[9]
[2]
[3]
[4]
[n]
30
27
..
15
12
9
6
3
40
36
..
20
16
12
8
4
90
81
..
45
36
27
18
9
100
90
..
50
40
30
20
10
[0] [1] [2] [8] [9]
20
18
..
10
8
6
4
2
30
27
..
15
12
9
6
3
40
36
..
20
16
12
8
4
20
18
..
10
8
6
4
30
27
..
15
12
9
6
20
18
..
10
8
6
4
2
30
27
..
15
12
9
6
3
40
36
..
20
16
12
8
4
90
81
..
45
36
27
18
9
100
90
..
50
40
30
20
10
90
81
..
45
36
27
18
9
100
90
..
50
40
30
20
10
…
[0]
[1]
[2]
Disclaimer
This is an educational presentation to enhance the
skill of computer science students.
This presentation is available for free to computer
science students.
Some internet images from different URLs are
used in this presentation to simplify technical
examples and correlate examples with the real
world.
We are grateful to owners of these URLs and
pictures.
© Rays Technologies 4210/14/16
Thank You!
© Rays Technologies 43
www.SunilOS.com
10/14/16

C# Basics

  • 1.
    © Rays Technologies1 C# BASICS www.sunilos.com www.raystec.com 10/14/16
  • 2.
    10/14/16 © RaysTechnologies 2 C# Object Oriented Programming Language Basic unit of C# is Object An Object is an instances of Class Class defines structure/skeleton of an Object The basic building blocks of C# is a Class so a C# program is nothing but a class
  • 3.
    10/14/16 © RaysTechnologies 3 C# Class contains methods and variables Variables contains values Methods perform operations A Class should have default method ‘Main’ that is entry point of an application
  • 4.
    10/14/16 © RaysTechnologies 4 Program Procedural Language int i = 5 //global variable void main(){ .. a(); } void a(int k){ int j = 0; //local variable .. }
  • 5.
    10/14/16 © RaysTechnologies 5 Procedural Language Library Library Program 1 Program 2 Program 3 Program 4
  • 6.
    10/14/16 © RaysTechnologies 6 Class C# Language int i = 5 //global variable void main(){ .. a(); } void a(int k){ int j = 0; //local variable .. }
  • 7.
    10/14/16 © RaysTechnologies 7 C # Library Namespace Class 1 Class 2 Class 3 Class 4
  • 8.
    10/14/16 © RaysTechnologies 8 C# Application ApplicationApplication Napspace 1 Namespace 2 Namespace 3 Namspace 4
  • 9.
    10/14/16 © RaysTechnologies 9 C# Program is a Class public class HelloCSharp{ ……. } A class may contain multiple variables and methods A Class should have default ‘Main’ method that is an entry point for an application
  • 10.
    10/14/16 © RaysTechnologies 10 My First Program - HelloCSharp  using System;  public class HelloCSharp { o public static void Main() { o String name = “Vijay”; o Console.WriteLine(“Hello” + name); o }  }  using, public, class, static, and void are keywords  Keywords are always written in small letters
  • 11.
    10/14/16 © RaysTechnologies 11 Compile & Run Compile o csc HelloCSharp.cs o It will generate HelloCSharp.exe Run o HelloCSharp
  • 12.
    10/14/16 © RaysTechnologies 12 Keywords class – is used to define a class public – Access modifier shows accessibility of a class or variable or method to other C# classes. There are 3 access modifiers public, protected and private static – Memory for the static variables are assigned only once in their life. Non-static variables are called instance variables void – is a NULL return type of Main method
  • 13.
    10/14/16 © RaysTechnologies 13 Commands Console.write()/Console.WriteLine() command is used to write text at standard output device String is a data type Two strings are concated by + operator o “Hello” + name
  • 14.
    10/14/16 © RaysTechnologies 14 Main method  The Main method is sometimes called the application's entry point.  Can be declared as  public static void Main(){}  OR  public static void Main(string args[]){}  OR public static int Main()  {… return errorCode}  OR  public static int Main(string args[])  {… return errorCode}
  • 15.
  • 16.
    10/14/16 © RaysTechnologies 16 Print Hello C# 5 times - while  using System;  public class HelloWhile {  public static void Main(String[] args) { o boolean isAlive = true; o int round = 0; o while (isAlive) { o Console.WriteLine(“Basanti will dance"); o round++; o If(round>500 )  isAlive =false; o }  }}
  • 17.
    © Rays Technologies For10 Rs 5 shots How Much? Okey!! 10/14/16 17
  • 18.
    © Rays Technologies ForLoop using System; public class HelloFor { public static void Main() { ofor (int hits = 1; hits <= 5; hits++) { Console.WriteLine( “Shot Balloon"); o} o} } 10/14/16 18
  • 19.
    10/14/16 © RaysTechnologies 19 Print Hello C# 5 times – do-while using System; public class HelloDoWhile { public static void Main() { o int i = 0; o do {  Console.WriteLine ( i+ “ Hello C#");  i++; o } while (i < 5); } }
  • 20.
    10/14/16 © RaysTechnologies 20 Add.java using System; public class Add { public static void Main(String[] args) { oint a = 5; oint b = 10; oint sum = a + b; oConsole.WriteLine ("Sum is " + sum); } }
  • 21.
    10/14/16 © RaysTechnologies 21 switch  public static void Main() {  String country = “India”;  switch(country) {  case “India” : o Console.WriteLine(“Language is Hindi"); o break;  case “US” :  case “England” : o Console.WriteLine(“Language is English”); o break;  default : o Console.WriteLine(“Don’t know language {0}”, country); o break;  }//switch  }//method
  • 22.
    10/14/16 © RaysTechnologies 22 Value Types int long sbyte short float double 1 2 4 8 4 8 -128, +127 -9.223E18, +9.223E18 -32768, +32767 -2147483648, +2147483647 +3.4 E+38 +1.7 E+308 TypeType Size ByteSize Byte RangeRange char 2 0, 65535 (Unicode) bool 1 true, false 0 0 0 0 0 0 0 false DefaultDefault
  • 23.
    10/14/16 © RaysTechnologies 23 Value Types uint ulong byte ushort decimal 1 2 4 8 16 0, 255 0 .. 264-1 0, 65535 0,4294967295 ± 1E-28 .. ±7.9E28 TypeType Size ByteSize Byte RangeRange 0 0 0 0 0 DefaultDefault
  • 24.
    10/14/16 © RaysTechnologies 24 Other Data Types Reference types (composite) o objects o arrays strings is a reference data type
  • 25.
    10/14/16 © RaysTechnologies 25 System.String class  public static void Main() {  String name = "Vijay Dinanth Chouhan";  Console.WriteLine(" String Length- " + name.Length);  Console.WriteLine(" 7 ths caharcter is- " + name[6]);  Console.WriteLine(" Dina index is- " + name.IndexOf("Dina"));  Console.WriteLine(" First i Position- " + name.IndexOf("i"));  Console.WriteLine(" Last i Position- " + name.LastIndexOf("i"));  Console.WriteLine(" a is replaced by b- " + name.Replace("a", "b"));  Console.WriteLine(" Chota vijay- " + name.ToLower());  Console.WriteLine(" Bada vijay- " + name.ToUpper());  Console.WriteLine(" Starts With Vijay- " + name.StartsWith("Vijay"));  Console.WriteLine(" Ends with han- " + name.EndsWith("han"));  Console.WriteLine(" Substring- " + name.Substring(6));  }
  • 26.
    10/14/16 © RaysTechnologies 26 System.Math class  public static void main(String[] args) {  Console.WriteLine("Math functions");  Console.WriteLine(" Max 2,5 - " + Math.Max(2,5));  Console.WriteLine(" Min 2,5 - " + Math.Min(2,5));  Console.WriteLine(" Absolute 3.7 - " + Math.Abs(3.7));  Console.WriteLine(" Exp 10 - " + Math.Exp(10));  Console.WriteLine(" Square Root- " + Math.Sqrt(4));  }
  • 27.
    10/14/16 © RaysTechnologies 27 Static vs Instance String name = “Vijay”; String surname = “Chohan” C.WL(name.Length); C.WL(surname.Length); String.Length C.WL(Math.Max(2,5)); C.WL(Math.Max(5,10));
  • 28.
    10/14/16 © RaysTechnologies 28 Hello <Name> public class HelloName { public static void Main(String[] args) {  Console.WriteLine("Hello " + args[0]); } }
  • 29.
    10/14/16 © RaysTechnologies 29 Hello Name – if <condition>  public class HelloName{  public static void Main(String[] args) { o if (args.Length == 1) {  Console.WriteLine("Hello " + args[0]); o } else {  Console.WriteLine("Usage : HelloName <name>"); o }  }  }
  • 30.
    10/14/16 © RaysTechnologies 30 Hello All public class HelloAll { public static void Main(String[] args) { o for (int i = 0; i < args.Length; i++) {  Console.WriteLine(i + " = Hello " + args[i]); o } } }
  • 31.
    10/14/16 © RaysTechnologies 31 Hello All - define Method  public static void Main(String[] args) { o PrintAll(args);  }// main  public static void PrintAll(String[] args) { o int size = args.Length; o for (int i = 0; i < size; i++) {  Console.WriteLine((i + 1) + " = Hello " + args[i]); o }  }//myMethod
  • 32.
    10/14/16 © RaysTechnologies 32 Add.java public class Add { public static void Main(String[] args) { o int a = Integer32.Parse(args[0]); o int b = Integer32.Parse (args[1]); o int sum = a + b; o Console.WriteLine("Sum is " + sum); } }
  • 33.
    10/14/16 © RaysTechnologies 33 Division public class Division { public static void Main(String[] args) { o int a = Integer32.Parse (args[0]); o int b = Integer32.Parse (args[1]); o double div = a/b; o Console.WriteLine("Division is " + div); } }
  • 34.
    10/14/16 © RaysTechnologies 34 Return a Value public class Division3 { public static void Main(String[] args) { o int a = Integer.Parse (args[0]); o int b = Integer.Parse (args[1]); o double div = getDivision(a, b); o Console.WriteLine("Division is " + div); }  public static double getDivision(int a, int b) { o double div = a / b; o return div;  }  }
  • 35.
    10/14/16 © RaysTechnologies 35 10 One Dimension Array 20 [0] 18 .. 10 8 6 4 2 [1] [8] [9] [2] [3] [4] [n] Length int[] table = new int[10]; int a = table[4]; int a = table[2]; int size = table.Length
  • 36.
    10/14/16 © RaysTechnologies 36 10 Initialize an Array 20 [0] 18 .. 10 8 6 4 2 [1] [8] [9] [2] [3] [4] [n] Length int[] table = new int[10]; table[0] =2; table[1] =4; …. Or int[] table = {2,4,6,8,10,12,14,16,18, 20};
  • 37.
    10/14/16 © RaysTechnologies 37 Other Data Type Arrays char[] chList = new char[5]; chList[0] = ‘A’…. Or char[] chList = {‘A’,’B’,’C’,’D’,’E’} String[] strList = new String[5]; strList[0] = “A” strList[1] = “Bee” Console.WriteLine(strList[0]); …. Or String[] strList = {“A”,”Bee”,”Cee”,”Dee”,”E”}
  • 38.
    10/14/16 © RaysTechnologies 3810length 2D Array [0] 20 18 .. 10 8 6 4 2 [1] [8] [9] [2] [3] [4] [n] 30 27 .. 15 12 9 6 3 40 36 .. 20 16 12 8 4 90 81 .. 45 36 27 18 9 100 90 .. 50 40 30 20 10 … [0] [1] [2] [7] [8] 9 9 .. 9 9 9 9 9
  • 39.
    10/14/16 © RaysTechnologies 39  int[][] table = new int[10][9];  table[1][5] = 5;  int size = table.Length;  int size = table[0].Length;  int[][] rows = new int[10][];  rows[0] = new int[9];  rows[1] = new int[19];  rows[2] = new int[29];  int xyz = new int[10][9][2];
  • 40.
    10/14/16 © RaysTechnologies 40 Rectangular Array
  • 41.
    10/14/16 © RaysTechnologies 41 3D Array 20 [0] 18 .. 10 8 6 4 2 [1] [8] [9] [2] [3] [4] [n] 30 27 .. 15 12 9 6 3 40 36 .. 20 16 12 8 4 90 81 .. 45 36 27 18 9 100 90 .. 50 40 30 20 10 [0] [1] [2] [8] [9] 20 18 .. 10 8 6 4 2 30 27 .. 15 12 9 6 3 40 36 .. 20 16 12 8 4 20 18 .. 10 8 6 4 30 27 .. 15 12 9 6 20 18 .. 10 8 6 4 2 30 27 .. 15 12 9 6 3 40 36 .. 20 16 12 8 4 90 81 .. 45 36 27 18 9 100 90 .. 50 40 30 20 10 90 81 .. 45 36 27 18 9 100 90 .. 50 40 30 20 10 … [0] [1] [2]
  • 42.
    Disclaimer This is aneducational presentation to enhance the skill of computer science students. This presentation is available for free to computer science students. Some internet images from different URLs are used in this presentation to simplify technical examples and correlate examples with the real world. We are grateful to owners of these URLs and pictures. © Rays Technologies 4210/14/16
  • 43.
    Thank You! © RaysTechnologies 43 www.SunilOS.com 10/14/16