C#.Net Unit-I
C#.Net Unit-I
G . S. College of Commerce,
Wardha
Department of B.Com Computer Application
BCCA Part-I Sem-II
C#.Net
Presented By
Prof. Amol Raut
Assistant Professor
Syllabus
Unit-I
Introduction to C#, An Overview of C#, Data Types, Literals, and Variables, Operators, Program control
statements, Introducing classes and objects, Arrays and strings, A closer look at methods and classes. Operator
Overloading, Inheritance, Interfaces, structure and Enumeration, Exception Handling
Unit-II
Delegates and Events, Applying Events: Namespaces, preprocessor and assemblies, Unsafe code, Pointers,
Miscellaneous keywords, Constructors, Overloaded Constructors, Static Constructors, Private Constructors,
Copy Constructors, Destructors
Unit-III
Introduction and Understanding .NET with C# Environment –The .NET Strategy, .NET Framework,
Origin of .NET Technology, The Common Langue Runtime, User and Program Interface, Visual
Studio .NET, .NET Languages, C# and .NET., Boxing and Unboxing, Passing String Objects to Write Line
Method, Multiple Main Methods, Compile Time Error, Type Conversion.
Unit-IV
Exploring the C# library, system Namespace, string and formatting, Multithreaded programming, Basic
Windows programming, Advanced Windows forms features, Deploying Windows Application.
Unit-I
Introducing C#:
NET (pronounced dot net) is a framework that provides a programming guidelines that can be used to
applications. ... NET framework can work with several programming languages such as C#, VB.NET, C++
Why C#
A large number of computer languages, starting from FORTAN developed in 1957 to the object-oriented
language java introduced in 1995,are being used for various applications.
The choice of a language depends upon many factors such as hardware environment, business
environment, user requirements and so on.
The primary motivation while developing each of these language has been the concern that it be able
handle the increasing complexity of programs that are robust, durable and maintainable.
Evolution of C#
The World Wide Web is growing everyday not only in terms of the number of
web sites but also in terms of volume and variety of information it contains.
The number of individuals and organizations using the Internet, which supports
the World Wide Web is increasing exponentially.
The internet is therefore in the mainstream of many business organization
today.
Microsoft Chairman Bill Gates, the architect of many innovative and path
breaking software products during the past two decades to develop a software
platform which will overcome these limitation and enable users to get
information anytime and anywhere using a natural interface.
The platform should be a collection of readily available Webservices that can
be distributed and accessed via standard Internet protocols.
The outcome is a new generation platform called .NET. .NET is simply the
Microsoft vision of software as a service.
Characteristics of C#
Application of C#
JIT- Just -in -Time
Jump statements:
• In C#, Jump statements are used to transfer control from one point to another point in the program due to
some specified code while executing the program. There are five keywords in the Jump Statements:
break
continue
goto
return
throw
break statement
The break statement is used to terminate the loop or statement in which it present. After that, the
control will pass to the statements that present after the break statement, if available. If the break
statement present in the nested loop, then it terminates only those loops which contains break
statement.
C# program to illustrate the
// use of break statement
using System;
class Geeks {
// Main Method
static public void Main()
{
Console.WriteLine(“abc");
}
}
}
continue statement
• This statement is used to skip over the execution part of the loop on a certain condition. After that, it
transfers the control to the beginning of the loop. Basically, it skips its following statements and continues
with the next iteration of the loop.
// C# program to illustrate the
// use of continue statement
using System;
class Geeks {
// Main Method
public static void Main()
{
Console.WriteLine(i);
}
}
}
goto statement
This statement is used to transfer control to the labeled statement in the program. The label is the valid
identifier and placed just before the statement from where the control is transferred.
// C# program to illustrate the
// use of goto statement
using System;
class Geeks {
// Main Method
static public void Main()
{
int number = 20;
switch (number) {
case 5:
Console.WriteLine("case 5");
break;
case 10:
Console.WriteLine("case 10");
break;
case 20:
Console.WriteLine("case 20");
// goto statement transfer
// the control to case 5
goto case 5;
default:
Console.WriteLine("No match found");
}
}
}
return statement
This statement terminates the execution of the method and returns the control to the calling method. It returns
an optional value. If the type of method is void, then the return statement can be excluded.
The index is starting from 0, which stores value. we can also store a fixed number of values in an array. Array
index is to be increased by 1 in sequence whenever its not reach the array size.
Array Declaration
Syntax :
< Data Type > [ ] < Name_Array >
Here, < Data Type > : It define the element type of the array. [ ] :
It define the size of the array.
< Name_Array > : It is the Name of array.
Example :
int[] x; // can store int values
string[] s; // can store string values
double[] d; // can store double values Student[]
stud1; // can store instances of Student class which is custom class
using System;
public class ArrayExample
{
public static void Main(string[] args)
{
int[] arr = new int[5];//creating array
arr[0] = 10;//initializing array
arr[2] = 20;
arr[4] = 30;
//traversing array
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine(arr[i]);
}
}
}
10
0
20
0
30
using System;
public class ArrayExample
{
public static void Main(string[] args)
{
int[] arr = { 10, 20, 30, 40, 50 };//
Declaration and Initialization of array
//traversing array
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine(arr[i]);
}
}
}
10
20
30
40
50
• The multidimensional array is also known as rectangular arrays in C#. It can be two dimensional or three
dimensional. The data is stored in tabular form (row * column) which is also known as matrix.
• To create multidimensional array, we need to use comma inside the square brackets.
• To create a 2D array, add each array within its own set of curly braces, and insert a comma (,) inside the
square brackets:
• For example:
//traversal
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
Console.Write(arr[i,j]+" ");
}
Console.WriteLine();//new line at each row
}
}
0 10 0
0 0 20
30 0 0
• Let's see a simple example of multidimensional array which initializes array at the time of declaration.
using System;
public class MultiArrayExample
{
public static void Main(string[] args)
{
int[,] arr = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };//declaration and initialization
//traversal
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
Console.Write(arr[i,j]+" ");
}
Console.WriteLine();//new line at each row
}
}
}
123
456
789
using System;
class Program
{
// User defined function
public void Show(int val)
{
val = val*val; // Manipulating value
Console.WriteLine("Value inside the show function "+val);
// No return statement
}
// Main function, execution entry point of the program
static void Main(string[] args)
{
int val = 50;
Program program = new Program(); // Creating Object
Console.WriteLine("Value before calling the function "+val);
program.Show(val); // Calling Function by passing value
Console.WriteLine("Value after calling the function " + val);
}
}
Passing by reference is a technique for passing parameters to a function. It is also known as call by reference, call by pointers,
and pass by pointers. C# provides a ref keyword to pass argument as reference-type. It passes reference of arguments to the function rather
than copy of original value.
using System;
class Program
{
static void Main(string[] args)
{
int x = 10;
Console.WriteLine("Variable Value Before Calling the Method: {0}", x);
Multiplication(ref x);
Console.WriteLine("Variable Value After Calling the Method: {0}", x);
Console.WriteLine("Press Enter Key to Exit..");
Console.ReadLine();
}
public static void Multiplication(ref int a)
{
a = a*a;
Console.WriteLine("Variable Value Inside the Method: {0}", a);
}
}
Method overloading
• A user can implement function overloading by defining two or more functions in a
class sharing the same name. C# can distinguish the methods with different method
signatures. i.e. the methods can have the same name but with different parameters
list (i.e. the number of the parameters, order of the parameters, and data types of the
parameters) within the same class.
• Overloaded methods are differentiated based on the number and type of the
parameters passed as arguments to the methods.
• Different ways of doing overloading methods-
Method overloading can be done by changing:
1.The number of parameters in two methods.
2.The data types of the parameters of methods.
3.The Order of the parameters of methods.
The concept of overloading a function can also be applied to operators. Operator
overloading gives the ability to use the same operator to do various operations.
It provides additional capabilities to C# operators when they are applied to user-defined
data types.
It enables to make user-defined implementations of various operations where one or both
of the operands are of a user-defined class.
Only the predefined set of C# operators can be overloaded. To make operations on a user-
defined data type is not as simple as the operations on a built-in data type.
To use operators with user-defined data types, they need to be overloaded according to a
programmer’s requirement.
An operator can be overloaded by defining a function to it. The function of the operator is
declared by using the operator keyword.
Syntax :
access specifier className operator Operator_symbol (parameters) { // Code }
operator (object);
here, operator is a symbol that denotes a unary operator.
operator a;
Example :
Input : -22, 18
Output : 22, -18
Overloading Binary Operators
• Binary Operators will work with two Operands.
• Examples of binary operators include the Arithmetic Operators (+, -, *, /, %),
• Arithmetic Assignment operators (+=, -+, *=, /+, %=) and Relational Operators etc.
• Overloading a binary operator is similar to overloading a unary operator, except that a binary operator
requires an additional parameter.
Syntax :
operator operator (object1, object2);
Here, second "operator" is a symbol that
denotes a binary operator.
operator + (a, b);
Example :
Input : 200, 40
Output : 240
Input : 300, 20
Output : 320
200
40
240
using System;
class Complex
{
private int x;
private int y;
public Complex()
}
public Complex(int i, int j)
{
x = i;
y = j;
}
public void ShowXY()
{
Console.WriteLine("{0} {1}", x, y);
}
public static Complex operator +(Complex c1, Complex c2)
{
Complex temp = new Complex();
temp.x = c1.x + c2.x;
temp.y = c1.y + c2.y;
return temp;
}
}
class MyClient
{
public static void Main()
{
Complex c1 = new Complex(10, 20);
c1.ShowXY(); // displays 10 & 20
Complex c2 = new Complex(20, 30);
c2.ShowXY(); // displays 20 & 30
Complex c3 = new Complex();
c3 = c1 + c2;
c3.ShowXY(); // dislplays 30 & 50
}
}
Inheritance:
Class Structure
1. Members of a class are private by default. 1. Members of a structure are public by default.
4. It is declared using the class keyword. 4. It is declared using the struct keyword.
6. NULL values are possible in Class. 6. NULL values are not possible.
7. Syntax: 7. Syntax:
class class_name{ struct structure_name{
data_member; type structure_member1;
member_function; type structure_member2;
}; };