C# INTRODUCTION
INTRODUCTION
C#, pronounced “C Sharp,” is one of the new
languages in the .NET framework being
C# Presentation, Spring 2003
implemented by Microsoft.
All .NET languages compile to a common
byte code (MSIL) making their integration
into programs written in different languages
easier.
HISTORY
C
C++
C# Presentation, Spring 2003
Developed by Anders Hejlsberg
• Turbo Pascal
• Delphi
• Visual J++
Released in 2001-2002
PREVIOUS PROBLEMS
Memory Leaks
Illegal Pointer References
C# Presentation, Spring 2003
Overly Complex Multiple-Inheritance
Static Linking
RESOLUTIONS
Garbage Collection
Threw out pointers
C# Presentation, Spring 2003
Single inheritance with Interfaces
Dynamic Linking
WHAT IS C#
Contrary to popular belief, C# is not simply a
clone of or replacement for Java
C# Presentation, Spring 2003
According to Anders Hejlsberg, Microsoft’s
Chief Architect, C# is a derivation of C++, C,
Java, Modula 2, and Smalltalk
EXAMPLE OF CODE
The code looks a lot like Java
C# Presentation, Spring 2003
public class Example
{
public static void Main(string[] args)
{
foreach (string s in args)
{
[Link](s);
}
}
}
FEATURES
OOP
Enumerators
C# Presentation, Spring 2003
Operator Overloading
Windows API Invocation
Structured Error Handling
Delegates
Namespaces
DESIGN GOALS
You can use C# to create
traditionalWindows client applications
XML Web services
distributed components
client-server applications
database applications, etc…
The language provides support for
strong type checking
array bounds checking
automatic garbage collection, etc…
DESIGN GOALS (CONTD.)
Intended to be suitable for writing
applications for both
hosted and embedded systems, ranging from the
very large that use sophisticated operating
systems, down to the very small having
dedicated functions.
C# Presentation, Spring 2003
C# PROGRAMMING BASICS
C# VARIABLES
Example: C# Variables
C# Presentation, Spring 2003
int num = 100;
float rate = 10.2f;
decimal amount = 100.50M;
char code = 'C';
bool isValid = true;
string name = "Steve";
C# VARIABLES
Thefollowings are naming conventions for
declaring variables in C#:
Variable names must be unique.
C# Presentation, Spring 2003
Variable names can contain letters, digits, and the
underscore _ only.
Variable names must start with a letter.
Variable names are case-sensitive, num and Num are
considered different names.
Variable names cannot contain reserved keywords. Must
prefix @ before keyword if want reserve keywords as
identifiers.
C# Presentation, Spring 2003
C# DATA TYPES
C# OPERATORS
•Arithmetic operators
•Assignment operators
•Comparison operators
•Equality operators
•Boolean logical operators
•Bitwise and shift operators
•Member access operators
•Type-cast operators
C# Presentation, Spring 2003
C# STATEMENTS
C# - IF, ELSE IF, ELSE
STATEMENTS
int a = 10, b = 20;
if (a>b)
C# Presentation, Spring 2003
[Link]("a is
greater"+a);
else
[Link]("b is greater"+b);
C# - IF, ELSE IF, ELSE
STATEMENTS
int i = 10, j = 20;
if (i == j)
C# Presentation, Spring 2003
[Link]("i is equal to j");
else if (i > j)
[Link]("i is greater than j");
else if (i < j)
[Link]("i is less than j");
C# -NESTED IF
int i = 10, j = 20;
if (i != j)
{
if (i < j)
{
[Link]("i is less than j");
}
else if (i > j)
{
[Link]("i is greater than j");
}
}
else
[Link]("i is equal to j");
C# - SWITCH STATEMENT
int x = 10;
switch (x)
{
case 5:
[Link]("Value of x is 5");
break;
case 10:
[Link]("Value of x is 10");
break;
case 15:
[Link]("Value of x is 15");
break;
default:
[Link]("Unknown value");
break;
}
C# - FOR LOOP
for (int i = 10; i > 0; i--)
{
[Link]("Value of i: {0}", i);
}
Output:
Value of i: 10
Value of i: 9
Value of i: 8
Value of i: 7
Value of i: 6
Value of i: 5
Value of i: 4
Value of i: 3
Value of i: 2
Value of i: 1
C# - WHILE LOOP
int i = 0;
while (true)
{
Output:
[Link]("i = {0}", i);
i=0
i=1
i=2
i++; i=3
i=4
i=5
if (i > 10) i=6
break; i=7
i=8
} i=9
i = 10
C# - DO WHILE LOOP
int i = 0;
do
{
[Link]("i = {0}", i);
i++;
Output:
i=0
if (i > 5)
i=1
break; i=2
i=3
i=4
} while (i < 10); i=5
C# ARRAYS
int[] intArray;
int[] intArray;
intArray = new int[5];
double[] doubleArray = new
double[5];
char[] charArray = new char[5];
bool[] boolArray = new bool[2];
string[] stringArray = new string[10];
C# ARRAYS
Initializing Array in C#
// Initialize a fixed array one item at a time
int[] staticIntArray = new int[3];
staticIntArray[0] = 1;
staticIntArray[1] = 3;
staticIntArray[2] = 5;
// Initialize a dynamic array items during
declaration
string[] strArray = new string[] { "Mahesh Chand", "Mike
Gold", "Raj Beniwal", "Praveen Kumar", "Dinesh Beniwal" };
ARRAY TYPES IN C#
Arrays can be divided into the following four
categories.
•Single-dimensional arrays
C# Presentation, Spring 2003
•Multidimensional arrays or rectangular arrays
•Jagged arrays
•Mixed arrays.
C# ARRAYS
//Example: Accessing Array Elements using for Loop
int[] evenNums = { 2, 4, 6, 8, 10 };
for (int i = 0; i < [Link]; i++)
[Link](evenNums[i]);
for (int i = 0; i < [Link]; i++)
evenNums[i] = evenNums[i] + 10; // update the value of each element by 10
C# ARRAYS
//Example: Accessing Array using foreach Loop
int[] evenNums = { 2, 4, 6, 8, 10};
string[] cities = { "Mumbai", "London", "New York" };
foreach (var item in evenNums)
[Link](item);
foreach (var city in cities)
[Link](city);
C# PASSING ARRAY AS ARGUMENT
int[] nums = { 1, 2, 3, 4, 5 };
UpdateArray(nums);
foreach (var item in nums)
[Link](item);
static void UpdateArray(int[] arr)
{
for (int i = 0; i < [Link]; i++)
arr[i] = arr[i] + 10;
}
TWO-DIMENSIONAL ARRAY IN C#
using System;
namespace MultiDArray
{
class Program
{
static void Main(string[] args)
{
//initializing 2D array
int[,] numbers = { { 1,2, 3 }, {3, 4, 5 } };
// access first element from the first row
[Link]("Element at index [0, 0] : " + numbers[0, 0]);
// access first element from second row
[Link]("Element at index [1, 0] : " + numbers[1, 0]);
}
}
}
C# JAGGED ARRAYS
In C#, jagged array is also known as "array of arrays" because its
elements are arrays. The element size of jagged array can be
different
public class JaggedArrayTest
{
public static void Main()
{
C# Presentation, Spring 2003
int[][] arr = new int[2][];// Declare the array
arr[0] = new int[] { 11, 21, 56, 78 };// Initialize the array
arr[1] = new int[] { 42, 61, 37, 41, 59, 63 };
// Traverse array elements
for (int i = 0; i < [Link]; i++)
{
for (int j = 0; j < arr[i].Length; j++)
{
[Link](arr[i][j]+" ");
}
[Link]();
}
}
}
C# ARRAYS- LINQ METHODS
//Example: LINQ Methods
int[] nums = new int[5] { 10, 15, 16, 8, 6 };
[Link](); // returns 16
[Link](); // returns 6
[Link](); // returns 55
[Link](); // returns 55
C# Array Class Example
using System;
namespace CSharpProgram
{
class Program
{
static void Main(string[] args)
{
C# Presentation, Spring 2003
// Creating an array
int[] arr = new int[6] { 5, 8, 9, 25, 0, 7 };
// Creating an empty array
int[] arr2 = new int[6];
// Displaying length of array
[Link]("length of first array: "+[Link]);
// Sorting array
[Link](arr);
[Link]("First array elements: ");
// Displaying sorted array
PrintArray(arr);
// Finding index of an array element
[Link]("\nIndex position of 25 is "+[Link](arr,25));
/ Coping first array to empty array
[Link](arr, arr2, [Link]);
[Link]("Second array elements: ");
// Displaying second array
PrintArray(arr2);
[Link](arr);
C# Presentation, Spring 2003
[Link]("\nFirst Array elements in reverse order: ");
PrintArray(arr);
}
// User defined method for iterating array elements
static void PrintArray(int[] arr)
{
foreach (Object elem in arr)
{
[Link](elem+" ");
}
}
}
}
C# STRING
Get the Length of a string
using System;
namespace CsharpString
{
class Test
{
public static void Main(string[] args)
C# Presentation, Spring 2003
{
// create string
string str = "C# Programming";
[Link]("string: " + str);
// get length of str
int length = [Link];
[Link]("Length: " + length);
[Link]();
}
}
}
C# STRING
C# compare two strings
using System;
namespace CsharpString
{
class Test
{
public static void Main(string[] args)
C# Presentation, Spring 2003
{
// create string
string str1 = "C# Programming";
string str2 = "C# Programming";
string str3 = "Programiz";
// compare str1 and str2
Boolean result1 = [Link](str2);
[Link]("string str1 and str2 are equal: " + result1);
//compare str1 and str3
Boolean result2 = [Link](str3);
[Link]("string str1 and str3 are equal: " + result2);
[Link](); string: C# Programming
} }} Length: 14