SlideShare a Scribd company logo
Microsoft .Net
(Introduction to C#)
Rohit Rao
Agenda
• Overview of C#
• Control Structures
• Arrays
• Generics
• Namespace
Overview of C#
• Object Oriented Programming Language
• Derived from C, C++ & Java
Control Structure
• Types
– Decision (Conditional) : Use to control the flow of program. Code runs
only once if certain condition is true.
– Iteration
• Decision
– If, if-else, If-else if
– Switch
• Iteration
– For Loop
– ForEach Loop
– While
– Do While
Decision Statements
• If statements
– If : Code runs once only if condition is true.
– If-Else : It allow us to specify the code that should
run if first statement (If) evaluates to false
– If-Else If-Else : It allow us to select from multiple if
statements.
– The ? : Operator :
• Exp1 ? Exp2 : Exp3
Switch
• It allow us to select from pre-defined set of
choices
• After each choice statement, we will have to use
break keyword
switch (i)
{
case 1:
Console.WriteLine(1);
break;
case 2:
Console.WriteLine(2);
break;
case 3:
Console.WriteLine(3);
break;
default:
Console.WriteLine("Please enter a number between 1 & 3.");
break;
}
Iterations
• It repeats the code in cyclic fashion
• For Loop
– It allow us to repeat the block of code N times
– We can use any decision statement in it
– We can use Continue / Break
int sum = 0;
for (int i = 0; i < 5; i++)
{
sum += i;
}
Console.WriteLine(sum);
For Each Loop
• Used to loop through items in a collection or
array individually (With no indexes)
• The collection or array must use interface
IEnumerable / IEnumerable <T>
• We can not add / remove from the collection
within the loop body
string[] students = { "A", "B", "C" };
foreach (string studentName in students)
{
Console.WriteLine(studentName);
}
While Loop
• Repeat the block of code while a certain
condition remains true.
int sum = 0;i = 0;
while (i < 5)
{
sum += i;
i++;
}
Console.WriteLine(sum);
Do-While Loop
• It tests the condition at the end of the loop Body
• Loop is guaranteed to execute at least once time.
int sum = 0;i = 0;
do
{
sum += i;
i++;
} while (i < 5);
Console.WriteLine(sum);
Continue Keyword
• Allow us to immediately move to next
iteration of a loop body
• It starts the next iteration immediately and
skip the steps below the Continue statement
in loop’s body.
Break Keyword
• It is used to move out from the iteration and it
does not loop any more.
• It skips the code below Break statement.
Array
• string[] arrOfStr = new string[] { "A", "B", "C" };
• string[] arrOfStr = new string[3];
arrOfStr[0] = "A";
arrOfStr[1] = "B";
arrOfStr[2] = "C";
• Passing Array To Method
//Method
public void ReadArray(int[] arr)
{ }
//Call
int[] arrOfInt = { 1, 2, 3 };
ReadArray(arrOfInt);
2D Array
string[,] arr2D= new string[,]
{
{"A", "1"},
{"B", "2"},
{"C", "3"},
{"D", "4"},
};
//Print
Console.WriteLine(arr2D[0, 0]);
int arr2D = new int[3, 3];
---------------------------------------------------------------------------------
Passing 2D array to method :
public void Pass2DArray(int[,] arr2d)
{
// Display value of first element in first row.
Console.WriteLine(arr2d[0, 0]);
}
Generics
• Arrays are strong typed
– No boxing / Unboxing (Adv)
– Fixed Length (DisAdv)
• Array List
– Variable / Flexible length (Adv)
– Lot of Boxing / UnBoxing because they take
objects (DisAdv)
• Generics provides advantages of both of these
collection : Strong Type & Flexible
• It creates Flexible Strong Typed Collection
Generics
• It separates the logic from Data Type, to
increase reusability
String
Boolean
Numeric
Generic compare logic (separate from data
type) for all the data types
-> Is “A” == “B” ?
-> Is bTrue == bFlase ?
-> Is 5 == 5 ?
At runtime attach the data type to the
method.
Generic Method Example
//Generic Method
public static bool Compare<T>(T x, T y)
{
if (x.Equals(y))
return true;
return false;
}
• //Method Call
bool result = Compare(10, 12);
Console.WriteLine(result.ToString());
result = Compare("A", "B");
Console.WriteLine(result.ToString());
result = Compare(true, true);
Console.WriteLine(result.ToString());
Console.ReadLine();
Generic Class Example
public class GenericCompare<T>
{
public bool Comapre(T x, T y)
{
if (x.Equals(y))
return true;
return false;
}
}
//Method call for integers
GenericCompare<int> objGenericCompInt = new GenericCompare<int>();
result = objGenericCompInt.Comapre(10, 12);
Console.WriteLine(result.ToString());
//Method Call for string
GenericCompare<String> objGenericCompString = new GenericCompare<String>();
result = objGenericCompString.Comapre("A", "B");
Console.WriteLine(result.ToString());
Generic Collections
• Generic decoupled datatype from Logic
• In .NET, Microsoft team took .NET collections & applied
generic concepts & created Generic Collections
• We will need to import System.Collections.Generic
Array List - > List (Generic) ---- Index Based
Hash Table - > Dictionary --- Key Value Pair
Stack & Queue - > Stack Generics & Queue Generics ---- Prioritized
Generic Collection (Index Based)
• List of Int
• List Of String
List<String> lstStr = new List<string>();
lstStr.Add("A");
lstStr.Add("B");
lstStr.Remove("A");
Generic Collections (Key Based)
• Dictionary : Generic form of HashTables
Dictionary<int, int> dictInt = new Dictionary<int, int>();
dictInt.Add(1, 1);
dictInt.Add(2, 2);
Dictionary<int, string> dictString = new Dictionary<int, string>();
dictString.Add(1, "A");
dictString.Add(2, "B");
Generic Collection (Stack / Queue)
• Stack
Stack<int> stkInt = new Stack<int>();
stkInt.Push(1);
stkInt.Push(2);
stkInt.Pop();
• Queue
Queue<string> QString = new Queue<string>();
QString.Enqueue("A");
QString.Enqueue("B");
QString.Dequeue();
QString.Dequeue();
Generic Collection (Custom Objects)
• We can attach custom objects to Generic
collections :
Student objStudent = new Student();
objStudent.StudentID = 1;
objStudent.RollNo = 1;
objStudent.StudentName = "A";
List<Student> lstStudent = new List<Student>();
lstStudent.Add(objStudent);
IEnumerable, ICollection, IList, IDictionary
• Interfaces : These all are interfaces, It provides
decoupling.
• Encapsulation : We can control how much
collection access should give to end clients.
• Polymorphism : We can dynamically point to
different collection on runtime.
IEnumerable, ICollection, IList, IDictionary
IEnumerator ICollection
IList or
IDictionary
Only Browse Browse + Count Browse + Count + Add +
Remove elements
IEnumerable, ICollection, IList, IDictionary
static void Main(string[] args)
{
//Fetch Students info
Student objStudent = new Student();
ArrayList arrLstStudents = objStudent.GetStudents();
//Here user can modify the arrLstStudents objects but it may be for browse only
purpose
/*
arrLstStudents.Remove(Object obj);
arrLstStudents.Add(Object obj);
*/
//It would be good if we can give him IEnumerable object instead of ArrayList.
So client will not be able to ad / remove items
IEnumerable studentList = objStudent.GetStudentsIEnumerable();
}
IEnumerable, ICollection, IList, IDictionary
public class Student
{
//Method to get array List
public ArrayList GetStudents()
{
ArrayList arrLstStudents = new ArrayList();
//Logic to fetch Student info & Add in ArrayList object
return arrLstStudents;
}
//Method to get IEnumerable
public IEnumerable GetStudentsIEnumerable()
{
ArrayList arrLstStudents = new ArrayList();
//Logic to fetch Student info & Add in ArrayList object
return CovertToIEnumerable(arrLstStudents);
}
public IEnumerable CovertToIEnumerable(ArrayList arrList)
{
return (IEnumerable) arrList;
}
}
NameSpace
• It helps you to organize your program / code.
• It Provides a way to create globally unique
types.
• It helps in avoiding class name clashes in more
than one set of code.
• The compiler adds a default namespace.
NameSpace Contd…

More Related Content

PPTX
Web-Dev Portfolio
PDF
JavaScript - Chapter 12 - Document Object Model
PDF
3. Java Script
PPTX
Javascript
PPTX
An Introduction to JavaScript
PPTX
Java script
Web-Dev Portfolio
JavaScript - Chapter 12 - Document Object Model
3. Java Script
Javascript
An Introduction to JavaScript
Java script

What's hot (20)

PDF
JavaScript - Chapter 8 - Objects
PPT
introduction to javascript
PPTX
Lecture 5 javascript
PPTX
Placement and variable 03 (js)
PPTX
Java script
PPTX
JavaScript Fundamentals & JQuery
PPTX
Javascript functions
PPTX
Javascript Basics by Bonny
PPT
Java Script ppt
PDF
Angular - Chapter 2 - TypeScript Programming
PPT
Learn javascript easy steps
PPTX
Introduction to JavaScript Basics.
PPT
Introduction to JavaScript
PPTX
Introduction to JavaScript Programming
PPTX
JS - Basics
PPTX
Introduction to JavaScript
PPT
Java script -23jan2015
PPTX
Javascript 101
PDF
JavaScript - Chapter 7 - Advanced Functions
JavaScript - Chapter 8 - Objects
introduction to javascript
Lecture 5 javascript
Placement and variable 03 (js)
Java script
JavaScript Fundamentals & JQuery
Javascript functions
Javascript Basics by Bonny
Java Script ppt
Angular - Chapter 2 - TypeScript Programming
Learn javascript easy steps
Introduction to JavaScript Basics.
Introduction to JavaScript
Introduction to JavaScript Programming
JS - Basics
Introduction to JavaScript
Java script -23jan2015
Javascript 101
JavaScript - Chapter 7 - Advanced Functions
Ad

Viewers also liked (20)

PPT
Programming in c#
PPTX
4. Introduction to ASP.NET MVC - Part I
PPTX
3. ADO.NET
PPTX
Microsoft .Net Framework
PPTX
4. introduction to Asp.Net MVC - Part II
PPT
0. Course Introduction
PPT
08. Numeral Systems
PPTX
C# Tutorial
PPT
14. Defining Classes
PPT
C# basics
PPT
20. Object-Oriented Programming Fundamental Principles
PPTX
Very basic of asp.net mvc with c#
PPT
17. Trees and Graphs
DOCX
cuaderno completo
PDF
Cartel manifa huelga 9 de mayo
PPTX
Trabajo slideshare
PDF
Help for Victims: Polish flyer
DOCX
PDF
De kracht van de vernieuwing
DOCX
Eucaristias semana de mision
Programming in c#
4. Introduction to ASP.NET MVC - Part I
3. ADO.NET
Microsoft .Net Framework
4. introduction to Asp.Net MVC - Part II
0. Course Introduction
08. Numeral Systems
C# Tutorial
14. Defining Classes
C# basics
20. Object-Oriented Programming Fundamental Principles
Very basic of asp.net mvc with c#
17. Trees and Graphs
cuaderno completo
Cartel manifa huelga 9 de mayo
Trabajo slideshare
Help for Victims: Polish flyer
De kracht van de vernieuwing
Eucaristias semana de mision
Ad

Similar to 2. overview of c# (20)

PDF
C# quick ref (bruce 2016)
PPTX
Collection
PPTX
Collections and its types in C# (with examples)
PPT
Generics collections
PDF
Dotnet programming concepts difference faqs- 2
PPTX
COLLECTIONS.pptx
PPTX
PPT
For Beginners - C#
PPTX
CSharp for Unity - Day 1
PPT
Generics Collections
PPTX
PROGRAMMING USING C#.NET SARASWATHI RAMALINGAM
PPSX
C# - Part 1
PPT
Introduction to csharp
PPT
Introduction to csharp
PPT
Introduction to csharp
PPT
Testing for share
PPTX
.Net Framework 2 fundamentals
PPTX
Collections and generics
PPTX
C# basics
C# quick ref (bruce 2016)
Collection
Collections and its types in C# (with examples)
Generics collections
Dotnet programming concepts difference faqs- 2
COLLECTIONS.pptx
For Beginners - C#
CSharp for Unity - Day 1
Generics Collections
PROGRAMMING USING C#.NET SARASWATHI RAMALINGAM
C# - Part 1
Introduction to csharp
Introduction to csharp
Introduction to csharp
Testing for share
.Net Framework 2 fundamentals
Collections and generics
C# basics

Recently uploaded (20)

PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Cloud computing and distributed systems.
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Approach and Philosophy of On baking technology
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PPT
Teaching material agriculture food technology
PDF
KodekX | Application Modernization Development
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Big Data Technologies - Introduction.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Electronic commerce courselecture one. Pdf
Network Security Unit 5.pdf for BCA BBA.
Cloud computing and distributed systems.
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Approach and Philosophy of On baking technology
Building Integrated photovoltaic BIPV_UPV.pdf
Machine learning based COVID-19 study performance prediction
Reach Out and Touch Someone: Haptics and Empathic Computing
MIND Revenue Release Quarter 2 2025 Press Release
Per capita expenditure prediction using model stacking based on satellite ima...
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Unlocking AI with Model Context Protocol (MCP)
Teaching material agriculture food technology
KodekX | Application Modernization Development
Encapsulation_ Review paper, used for researhc scholars
Diabetes mellitus diagnosis method based random forest with bat algorithm
Programs and apps: productivity, graphics, security and other tools
Big Data Technologies - Introduction.pptx

2. overview of c#

  • 2. Agenda • Overview of C# • Control Structures • Arrays • Generics • Namespace
  • 3. Overview of C# • Object Oriented Programming Language • Derived from C, C++ & Java
  • 4. Control Structure • Types – Decision (Conditional) : Use to control the flow of program. Code runs only once if certain condition is true. – Iteration • Decision – If, if-else, If-else if – Switch • Iteration – For Loop – ForEach Loop – While – Do While
  • 5. Decision Statements • If statements – If : Code runs once only if condition is true. – If-Else : It allow us to specify the code that should run if first statement (If) evaluates to false – If-Else If-Else : It allow us to select from multiple if statements. – The ? : Operator : • Exp1 ? Exp2 : Exp3
  • 6. Switch • It allow us to select from pre-defined set of choices • After each choice statement, we will have to use break keyword switch (i) { case 1: Console.WriteLine(1); break; case 2: Console.WriteLine(2); break; case 3: Console.WriteLine(3); break; default: Console.WriteLine("Please enter a number between 1 & 3."); break; }
  • 7. Iterations • It repeats the code in cyclic fashion • For Loop – It allow us to repeat the block of code N times – We can use any decision statement in it – We can use Continue / Break int sum = 0; for (int i = 0; i < 5; i++) { sum += i; } Console.WriteLine(sum);
  • 8. For Each Loop • Used to loop through items in a collection or array individually (With no indexes) • The collection or array must use interface IEnumerable / IEnumerable <T> • We can not add / remove from the collection within the loop body string[] students = { "A", "B", "C" }; foreach (string studentName in students) { Console.WriteLine(studentName); }
  • 9. While Loop • Repeat the block of code while a certain condition remains true. int sum = 0;i = 0; while (i < 5) { sum += i; i++; } Console.WriteLine(sum);
  • 10. Do-While Loop • It tests the condition at the end of the loop Body • Loop is guaranteed to execute at least once time. int sum = 0;i = 0; do { sum += i; i++; } while (i < 5); Console.WriteLine(sum);
  • 11. Continue Keyword • Allow us to immediately move to next iteration of a loop body • It starts the next iteration immediately and skip the steps below the Continue statement in loop’s body.
  • 12. Break Keyword • It is used to move out from the iteration and it does not loop any more. • It skips the code below Break statement.
  • 13. Array • string[] arrOfStr = new string[] { "A", "B", "C" }; • string[] arrOfStr = new string[3]; arrOfStr[0] = "A"; arrOfStr[1] = "B"; arrOfStr[2] = "C"; • Passing Array To Method //Method public void ReadArray(int[] arr) { } //Call int[] arrOfInt = { 1, 2, 3 }; ReadArray(arrOfInt);
  • 14. 2D Array string[,] arr2D= new string[,] { {"A", "1"}, {"B", "2"}, {"C", "3"}, {"D", "4"}, }; //Print Console.WriteLine(arr2D[0, 0]); int arr2D = new int[3, 3]; --------------------------------------------------------------------------------- Passing 2D array to method : public void Pass2DArray(int[,] arr2d) { // Display value of first element in first row. Console.WriteLine(arr2d[0, 0]); }
  • 15. Generics • Arrays are strong typed – No boxing / Unboxing (Adv) – Fixed Length (DisAdv) • Array List – Variable / Flexible length (Adv) – Lot of Boxing / UnBoxing because they take objects (DisAdv) • Generics provides advantages of both of these collection : Strong Type & Flexible • It creates Flexible Strong Typed Collection
  • 16. Generics • It separates the logic from Data Type, to increase reusability String Boolean Numeric Generic compare logic (separate from data type) for all the data types -> Is “A” == “B” ? -> Is bTrue == bFlase ? -> Is 5 == 5 ? At runtime attach the data type to the method.
  • 17. Generic Method Example //Generic Method public static bool Compare<T>(T x, T y) { if (x.Equals(y)) return true; return false; } • //Method Call bool result = Compare(10, 12); Console.WriteLine(result.ToString()); result = Compare("A", "B"); Console.WriteLine(result.ToString()); result = Compare(true, true); Console.WriteLine(result.ToString()); Console.ReadLine();
  • 18. Generic Class Example public class GenericCompare<T> { public bool Comapre(T x, T y) { if (x.Equals(y)) return true; return false; } } //Method call for integers GenericCompare<int> objGenericCompInt = new GenericCompare<int>(); result = objGenericCompInt.Comapre(10, 12); Console.WriteLine(result.ToString()); //Method Call for string GenericCompare<String> objGenericCompString = new GenericCompare<String>(); result = objGenericCompString.Comapre("A", "B"); Console.WriteLine(result.ToString());
  • 19. Generic Collections • Generic decoupled datatype from Logic • In .NET, Microsoft team took .NET collections & applied generic concepts & created Generic Collections • We will need to import System.Collections.Generic Array List - > List (Generic) ---- Index Based Hash Table - > Dictionary --- Key Value Pair Stack & Queue - > Stack Generics & Queue Generics ---- Prioritized
  • 20. Generic Collection (Index Based) • List of Int • List Of String List<String> lstStr = new List<string>(); lstStr.Add("A"); lstStr.Add("B"); lstStr.Remove("A");
  • 21. Generic Collections (Key Based) • Dictionary : Generic form of HashTables Dictionary<int, int> dictInt = new Dictionary<int, int>(); dictInt.Add(1, 1); dictInt.Add(2, 2); Dictionary<int, string> dictString = new Dictionary<int, string>(); dictString.Add(1, "A"); dictString.Add(2, "B");
  • 22. Generic Collection (Stack / Queue) • Stack Stack<int> stkInt = new Stack<int>(); stkInt.Push(1); stkInt.Push(2); stkInt.Pop(); • Queue Queue<string> QString = new Queue<string>(); QString.Enqueue("A"); QString.Enqueue("B"); QString.Dequeue(); QString.Dequeue();
  • 23. Generic Collection (Custom Objects) • We can attach custom objects to Generic collections : Student objStudent = new Student(); objStudent.StudentID = 1; objStudent.RollNo = 1; objStudent.StudentName = "A"; List<Student> lstStudent = new List<Student>(); lstStudent.Add(objStudent);
  • 24. IEnumerable, ICollection, IList, IDictionary • Interfaces : These all are interfaces, It provides decoupling. • Encapsulation : We can control how much collection access should give to end clients. • Polymorphism : We can dynamically point to different collection on runtime.
  • 25. IEnumerable, ICollection, IList, IDictionary IEnumerator ICollection IList or IDictionary Only Browse Browse + Count Browse + Count + Add + Remove elements
  • 26. IEnumerable, ICollection, IList, IDictionary static void Main(string[] args) { //Fetch Students info Student objStudent = new Student(); ArrayList arrLstStudents = objStudent.GetStudents(); //Here user can modify the arrLstStudents objects but it may be for browse only purpose /* arrLstStudents.Remove(Object obj); arrLstStudents.Add(Object obj); */ //It would be good if we can give him IEnumerable object instead of ArrayList. So client will not be able to ad / remove items IEnumerable studentList = objStudent.GetStudentsIEnumerable(); }
  • 27. IEnumerable, ICollection, IList, IDictionary public class Student { //Method to get array List public ArrayList GetStudents() { ArrayList arrLstStudents = new ArrayList(); //Logic to fetch Student info & Add in ArrayList object return arrLstStudents; } //Method to get IEnumerable public IEnumerable GetStudentsIEnumerable() { ArrayList arrLstStudents = new ArrayList(); //Logic to fetch Student info & Add in ArrayList object return CovertToIEnumerable(arrLstStudents); } public IEnumerable CovertToIEnumerable(ArrayList arrList) { return (IEnumerable) arrList; } }
  • 28. NameSpace • It helps you to organize your program / code. • It Provides a way to create globally unique types. • It helps in avoiding class name clashes in more than one set of code. • The compiler adds a default namespace.