Www Scribd
Www Scribd
Document Information
This document introduces C# programming langua…
Download
Download as pdf or txt
Chapter: 1
Introduction to C#.NET
Introduce C#
C#' (pronounced as 'C sharp') is a new computer-programming
language developed by Microsoft Corporation, USA. C# is a fully
object-oriented language like Java and is the first Component-
oriented language. It has been designed to support the key
features of .NET Framework, the new development platform of
Microsoft for building component-based software solutions. It is a
simple, efficient, productive and type-safe language derived from
the popular C and C++ languages. Although it belongs to the family
of C/C++, it is a purely objected-oriented, modern language
suitable for developing Web based applications.
'C#' seems a strange name for a modern programming language. Perhaps Microsoft named
their new language 'C sharp' because they wanted it to be better, smarter and 'sharper' than
its ancestors C and C++, C# is designed to bring rapid development to C++ programmers
without sacrificing the power and control that have been the hallmarks of C and C++, C# is the
only language designed especially for the .NET platform which provides tools and services that
fully exploit both computing and communications.
Since one of the designers of C# (Anders Hejsberg) was a Java expert, it is natural that many
Java features have been incorporated into the design of C#. In fact, in many cases, the C# code
may bear a striking resemblance to the functionally equivalent Java code. Unlike C++, both
Java and C# promote a one-stop coding approach to code maintenance. They group classes,
interfaces and implementations together in one file so that programmers can edit the code
more easily.
C# is designed for building robust, reliable and durable components to handle real-world
applications. Major highlights of C# are:
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
C# Features
The language that is designed for both computing and communication is characterised by
• Simple
• Object Oriented
Upload•
your documents to download.
Compatible
Consistent
•
• Modern
• Versionable
OR
Object-Oriented
C# is truly object-oriented. It supports all the three tenets of object-oriented systems,
namely,
o Encapsulation
Inheritance
Become a Scribd member to read and download full
o
o Polymorphism
Compatible
documents.
C# enforces the .NET common language specifications and therefore allows inter-
operation with other .NET languages. C# provides support for transparent access to
standard COM and OLE Automation. C# also permits interoperation with C-style APIs.
Modern
C# is called a modern language due to a number of features it supports. It supports
Vesionable
Making new versions of software modules work with the existing applications is known
as versioning. C# provides support for versioning with the help of new and override
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
keywords. With this support, a programmer can guarantee that his new class library will
maintain binary compatibility with the existing client applications.
OR
products during the past two decades, wanted to develop a software platform which will
overcome these limitations and enable users to get information anytime and anywhere, using
a natural interface. The platform should be a collection of readily available Web services that
can be distributed and accessed via standard Internet protocols. He wanted to make the Web
both programmable and intelligent. The outcome is a new generation platform called .NET.
NET is simply the Microsoft's vision of 'software as a service.
BecomeAlthough
a Scribd member to read and download full
the research and development work of .NET
documents.
platform began in the mid-90s, only during the
Microsoft Professional Developers Conference in
September 2000, was NET officially announced to the
developer community. At the same conference,
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
o How of algorithms
This would greatly help maintaining the program.
Upload your
An interface is similar documents
to a class tomembers.
but contains only abstract download.
Interfaces are used when
we want to implement the concept of multiple inheritance in a program.
A C# program may contain multiple class definitions. Classes are the primary and essential
Upload to Download
elements of a C# program. These classes are used to map the objects of real-world problems.
The number of classes depends on the complexity of the problem.
Since every C# application program requires a Main method as its starting point, the class
containing the Main is the essential part of the program. A simple C# program may contain
only this part. The Main method creates objects of various classes and establishes
communications between them. On reaching the end of Main, the program terminates and
the control passes back to the operating system. OR
The code below is the basic structure of a C# program.
Become a Scribd
class SampleOne{member to read and download full
public static void Main(){
}
documents.
Console.WriteLine("C# is Sharper than C++.");
Class Declaration
Start your 30 day free trial
The first line,
class SampleOne
The Braces
C# is a block-structured language, meaning code blocks are always enclosed by braces { and }.
Therefore, every class definition in C# begins with an opening brace ‘{’ and ends with a
corresponding closing brace ‘}’ that appears in the last line of the program.
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
defines a method named Main. Every C# executable program must include the Main() method
in one of the classes. This is the 'starting point for executing the program. A C# application can
a. public
The keyword public is an access modifier that tells the C# compiler that the Main
Upload to Download
method is accessible by anyone
b. static
The keyword static declares that the Main method is a global one and can be
called without creating an instance of the class. The compiler stores the address
of the method as the entry point and uses this information to begin execution
before any objects are created.
OR
c. void
The keyword void is a type modifier that states that the Main method does not
return any value (but simply prints some text to the screen).
Note the semicolon at the end of the statement. Every C# statement must end with a
semicolon. And also note that there is no semicolon at the end of the class.
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
A variable name can be chosen by the programmer in a meaningful way so as to reflect what
o Average
o total_height
As mentioned earlier, variable names may consist of alphabets, digits and the underscore (__),
subject to the following conditions:
documents.
3. The place of declaration (in the program) decides the scope of the variable. A variable
must be declared before it is used in the program.
A variable can be used to store a value of any data type. The general form of declaration
Start your 30 day free trial
of a variable is:
float p1, p2
char character1;
A variable must be given a value after it has been declared but before it is used in an
expression. A simple method of giving value to a variable is through the assignment
statement as follows:
variableName = value;
or,
Example:
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
value2 = 26;
1.4
You're Reading a Preview
Describe the Identifiers of C#
Upload to Download
o They must not begin with a digit
o Upper case and lower-case letters are distinct
o Keywords in stand-alone mode cannot be used as identifiers
C# permits the use of keywords as identifiers when they are prefixed with the '@' character
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
1. Value Type
2. Reference Type
3. Pointer Upload to Download
1. Value types
Value types (which are of fixed length) are stored on the stack, and when a value of a
variable is assigned to another variable, the value is actually copied. This means that two
identical copies of the value are available in memory.
OR
Int
The int data type can store whole numbers from -2147483648 to 2147483647. In general,
and in our tutorial, the int data type is the preferred data type when we create variables
documents.
int myNum = 100000;
Console.WriteLine(myNum);
Float
The float and double data types can store fractional numbers. Note that you should end
the value with an "F" for floats and "D" for doubles:
Example
float myNum = 5.75F;
Console.WriteLine(myNum);
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
Double
It is 64-bit double-precision floating point type. It has 14 – 15 digit Precision. To initialize a
double d1 = 324.2D;
Booleans
A boolean data type is declared with the bool keyword and can only take the values true
Upload to Download
or false:
Example
bool isCSharpFun = true;
Console.WriteLine(isCSharpFun); // Outputs True
Characters
The char data type is used to store a single character. The character must be surrounded
OR
by single quotes, like 'A' or 'c':
Example
documents.
Strings
The string data type is used to store a sequence of characters (text). String values must be
Console.WriteLine(greeting);
Decimal Types
The decimal type is a 128-bit data type suitable for financial and monetary calculations. It
has 28–29-digit Precision.
Example
decimal sa = 72847;
Console.WriteLine(sa);
2. Reference types
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
10
Reference types (which are of variable length) are stored on the heap, and when an
assignment between two reference variables occurs, only the reference is copied; the
User-defined reference types refer to those types which we define using predefined types.
a. Classes
b. Delegates
c. Interfaces
d. Arrays
Upload to Download
3. Pointer
A third category of types called pointers is available for use only in unsafe code. Value
types and reference types are further classified as predefined and user defined types
- & (ampersand) is the address operator used to determine the address of a
variable.
- * (asterisk) is indirection operator used to access the value of an address.
OR
documents.
Casting. Type casting is when you assign a value of one data type to another type.
The process of converting the value of one type (int, float, double, etc.) to another type is
known as type conversion.
Example:
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
11
short sa = 26;
using System;
namespace MyApplication
{
class Program
{
Upload to Download
static void Main(string[] args)
{
int numInt = 500;
documents.
// Value before conversion
Console.WriteLine("numInt value: " + numInt);
Console.WriteLine("numInt Type: " + n);
Output:
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
12
Explicit casting must be done manually by placing the type in parentheses in front of the
Example:
using System;
namespace MyApplication {
Become a Scribd
class Programmember
{
static void Main(string[] args) {
to read and download full
double numDouble = 1.23; documents.
// Explicit casting
int numInt = (int) numDouble;
Start your 30 day free trial
// Value before conversion
Console.WriteLine("Original double Value: "+numDouble);
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
13
A. Arithmetic operators
B. Relational operators
C.
D.
Logical operators
Assignment operators Upload to Download
E. Conditional operators
F. Bitwise operators
G. Miscellaneous operators
OR
A. Arithmetic Operator
Arithmetic operators are used to perform arithmetic operations such as addition,
subtraction, multiplication, division, etc. C# provides all the basic arithmetic operators.
The operators +, -, and / all work the same way as they do in other languages.
documents.
* Multiplies both operands A * B = 200
/ Divides numerator by de-numerator B/A=2
% Modulus Operator and remainder of after an integer division B%A=0
++ Increment operator increases integer value by one A++ = 11
Code:
using System;
namespace Operators
{
class Program
{
static void Main(string[] args)
{
int a = 21;
int b = 10;
int c;
c = a + b;
Console.WriteLine("Line 1 - Value of c is {0}", c);
c = a - b;
Console.WriteLine("Line 2 - Value of c is {0}", c);
c = a * b;
Console.WriteLine("Line 3 - Value of c is {0}", c);
c = a / b;
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
14
}
}
Output:
Line 1 - Value of c is 31 Upload to Download
Line 2 - Value of c is 11
Line 3 - Value of c is 210
Line 4 - Value of c is 2
Line 5 - Value of c is 1
OR
Line 6 - Value of c is 21
Line 7 - Value of c is 22
B. Relation Operator
Relational operators are used to check the relationship between two operands. If the
relationship is true the result will be true, otherwise it will result in false.
Become aRelational
Scribd member
operators tomaking
are used in decision read and download full
and loops.
documents.
Operator Description Example
== Two operands are equal or not, if yes then condition (A == B) is not true.
becomes true.
!= Two operands are equal or not, if values are not equal (A != B) is true.
then condition becomes true.
>
Start your 30 day free trial
Left operand is greater than the value of right operand,
if yes then condition becomes true.
(A > B) is not true.
< Left operand is less than the value of right operand, if (A < B) is true.
yes then condition becomes true.
>= Left operand is greater than or equal to the value of right (A >= B) is not true.
operand, if yes then condition becomes true.
<= Left operand is less than or equal to the value of right (A <= B) is true.
operand, if yes then condition becomes true.
Code:
using System;
class Program
{
static void Main(string[] args)
{
int a = 21;
int b = 10;
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
15
if (a == b)
{
if (a < b)
{
Console.WriteLine("Line 2 - a is less than b");
Upload to Download
}
else
{
Console.WriteLine("Line 2 - a is not less than b");
}
OR
if (a > b)
{
Console.WriteLine("Line 3 - a is greater than b");
}
else
{
documents.
/* Lets change value of a and b */
a = 5;
b = 20;
if (b >= a)
{
Console.WriteLine("Line 5-b is either greater than or equal to b");
}
}
}
Output:
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
16
C. Logical Operator
Upload
&&
your documents to download.
If both the operands are non-zero then condition (A && B) is false.
becomes true.
|| If any of the two operands is non zero then condition (A || B) is true.
becomes true.
! Use to reverses the logical state of its operand. If a !(A && B) is true.
Upload to Download
condition is true then Logical NOT operator will make
false.
Code:
using System;
namespace OperatorsAppl { OR
class Program {
static void Main(string[] args) {
bool a = true;
if (a && b) {
} documents.
Console.WriteLine("Line 1 - Condition is true");
if (a || b) {
Console.WriteLine("Line 2 - Condition is true");
Start your 30 day free trial
}
if (a && b) {
Console.WriteLine("Line 3 - Condition is true");
} else {
Console.WriteLine("Line 3 - Condition is not true");
}
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
17
Output:
Upload youraredocuments
D. Assignment Operator
Assignment operators used to assign the value ofto download.
an expression to a variable. We have
seen the usual assignment operator, '='.
Upload to Download
= Assigns values from right side operands to C = A + B assigns value of A + B into C
left side operand
+= It adds right operand to the left operand and C += A is equivalent to C = C + A
assign the result to left operand
-= It subtracts right operand from the left C -= A is equivalent to C = C - A
operand and assign the result to left
operand
*= It multiplies right operand with the left
operand and assign the result to left
OR C *= A is equivalent to C = C * A
operand
/= It divides left operand with the right C /= A is equivalent to C = C / A
operand and assign the result to left
Code:
using System;
namespace OperatorsAppl
{
class Program
{
static void Main(string[] args)
{
int a = 21;
int c;
c = a;
Console.WriteLine("Line 1 - = Value of c = {0}", c);
c += a;
Console.WriteLine("Line 2 - += Value of c = {0}", c);
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
18
c -= a;
c *= a;
Console.WriteLine("Line 4 - *= Value of c = {0}", c);
UploadcConsole.WriteLine("Line
your documents
/= a;
to download.
5 - /= Value of c = {0}", c);
c = 200;
c %= a;
Upload to Download
Console.WriteLine("Line 6 - %= Value of c = {0}", c);
c <<= 2;
Console.WriteLine("Line 7 - <<= Value of c = {0}", c);
c >>= 2;
OR
Console.WriteLine("Line 8 - >>= Value of c = {0}", c);
c &= 2;
Console.WriteLine("Line 9 - &= Value of c = {0}", c);
c ^= 2;
c |= 2;
documents.
Console.WriteLine("Line
Console.ReadLine();
11 - |= Value of c = {0}", c);
}
}
Output:
}
Start your 30 day free trial
Line 1 - = Value of c = 21
Line 2 - += Value of c = 42
Line 3 - -= Value of c = 21
Line 4 - *= Value of c = 441
Line 5 - /= Value of c = 21
Line 6 - %= Value of c = 11
Line 7 - <<= Value of c = 44
Line 8 - >>= Value of c = 11
Line 9 - &= Value of c = 2
Line 10 - ^= Value of c = 0
Line 11 - |= Value of c = 2
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
19
E. Conditional Operator
Ternary operator is a Conditional operator in C#. It takes three arguments and evaluates
b = (a == 1) ? 20 : 30;
Above, if the first operand evaluates to true (1), the second operand is evaluated. If the
Upload your documents to download.
first operand evaluates to false (0), the third operand is evaluated.
Code:
using System;
namespace DEMO {
class Program {
Upload to Download
static void Main(string[] args) {
int a, b;
a = 10;
b = (a == 1) ? 20 : 30;
Console.WriteLine("Value of b is {0}", b);
OR
b = (a == 10) ? 20 : 30;
Console.WriteLine("Value of b is {0}", b);
Console.ReadLine();
}
Become a Scribd
}
}
member to read and download full
Output: documents.
Value of b is 30
Value of b is 20
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
20
c = a | b; /* 61 = 0011 1101 */
Console.WriteLine("Line 2 - Value of c is {0}", c); OR
c = a ^ b; /* 49 = 0011 0001 */
Console.WriteLine("Line 3 - Value of c is {0}", c);
Become a Scribd
c = ~a;
member to read and download full
/*-61 = 1100 0011 */
Console.WriteLine("Line 4 - Value of c is {0}", c);
c = a << 2; documents.
/* 240 = 1111 0000 */
Console.WriteLine("Line 5 - Value of c is {0}", c);
Output:
Line 1 - Value of c is 12
Line 2 - Value of c is 61
Line 3 - Value of c is 49
Line 4 - Value of c is -61
Line 5 - Value of c is 240
Line 6 - Value of c is 15
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
21
G. Miscellaneous Operator
as
Upload to Download
certain type.
Cast without raising an exception if the
object of the Car class.
Object obj = new
cast fails. StringReader("Hello");
StringReader r = obj as StringReader;
Example:
using System;
namespace OperatorsAppl {
OR
class Program {
b = (a == 10) ? 20 : 30;
Console.WriteLine("Value of b is {0}", b);
Console.ReadLine();
}
}
}
Output:
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
22
Chapter: 2
Upload to Download
branching.
1. if statement
2. if else statement
3.
4.
if else if else statement
switch statement OR
2.2 Demonstrate if, if else and if else ladder and compute it
Become1. aif statement
Scribd member to read and download full
The if statement checks the given condition. If the condition evaluates to be true then the
documents.
block of code/statements will execute otherwise not.
Syntax:
if(condition)
{
Start your 30 day free trial
//statements
}
Code:
using System;
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
23
Output:
If statement executed.
Syntax:
if(condition)
{
Upload to Download
// code if condition is true
}
else
{
}
// code if condition is false
OR
Code:
using System;
documents.
public static void Main(string[] args)
{
string name = "Sam";
if (name == "Sam") {
Console.WriteLine("If statement executed.");
Start your 30 day free trial
}
else {
Console.WriteLine("Else statement executed.");
}
}
}
Output:
Else statement executed.
if(condition1)
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
24
{
// code to be executed if condition1 is true
Code: OR
using System;
Become a Scribd
class GFG {
member to read and download full
public static void Main(String[] args)
{
int i = 20;
documents.
if (i == 10)
Output:
i is 20
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
25
Syntax:
switch (expression)
Upload to Download
{
case value1: // statement sequence
break;
OR
case value2: // statement sequence
break;
.
.
.
case valueN: // statement sequence
Become a Scribd member to read and download full
break;
default: // default statement sequence
documents.
}
Code:
using System;
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
26
Output:
-
Start your 30 day free trial
Initialization of loop variable: The expression / variable controlling the loop is
initialized here. It is the starting point of for loop.
- Testing Condition: It is used for testing the exit condition for a loop. It must return
a boolean value true or false.
Syntax:
for (loop variable initialization ; testing condition; increment / decrement)
{
// statements to be executed
}
Code:
using System;
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
27
class forLoopDemo
{
Output:
Sam 1 Upload to Download
Sam 2
Sam 3
Sam 4
Syntax:
documents.
while (boolean condition)
{
loop statements...
}
Code:
Start your 30 day free trial
using System;
class whileLoopDemo
{
public static void Main()
{
int x = 1;
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
28
Output:
Sam 1
Upload
3. For each
your documents to download.
C# provides an easy to use and more readable alternative to for loop, the foreach loop
when working with arrays and collections to iterate through the items of
arrays/collections. The foreach loop iterates through each item, hence called foreach
loop.
Syntax:
Upload to Download
foreach(data_type var_name in collection_variable)
{
// statements to be executed
}
Code:
OR
using System;
class GFG {
documents.
static public void Main()
Console.WriteLine("Print array:");
Console.WriteLine(items);
Output:
Print array:
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
29
Syntax:
do
{
statements..
OR
} while (condition);
Code:
Become a Scribd
using System;
member to read and download full
class dowhileloopDemo
{ documents.
public static void Main()
{
int x = 21;
Start your 30 day free trial
do
{
// The line will be printed even
// if the condition is false
Console.WriteLine("Sam");
x++;
}
while (x < 20);
}
}
Output:
Sam
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
30
Loop control statements change execution from its normal sequence. When execution leaves
a scope, all automatic objects that were created in that scope are destroyed.
2. break statement
Terminates the loop or switch statement and transfers execution to the statement
Syntax:
break;
Code:
using System;
namespace Loops {
Upload to Download
class Program {
static void Main(string[] args) {
/* local variable definition */
int a = 10;
documents.
/* terminate the loop using break statement */
break;
}
}
Output:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
3. continue statement
Causes the loop to skip the remainder of its body and immediately retest its condition
prior to reiterating.
Syntax:
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
31
continue;
/* do loop execution */
do {
Upload to Download
if (a == 15) {
/* skip the iteration */
a = a + 1;
continue;
}
Console.WriteLine("value of a: {0}", a);
a++; OR
}
while (a < 20);
Console.ReadLine();
Become a Scribd
}
}
member to read and download full
}
Output:
documents.
value of a: 10
value of a: 11
4. goto statement
The C# goto statement is also known jump statement. It is used to transfer control to the
other part of the program. It unconditionally jumps to the specified label.
It can be used to transfer control from deeply nested loop or switch case label.
Code:
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
32
using System;
public class GotoExample
} Upload to Download
goto ineligible;
else
{
Console.WriteLine("You are eligible to vote!");
}
}
}
OR
Output:
You are not eligible to vote!
Enter your age:
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
33
Chapter: 3
Array
You're Reading a Preview
3.1 Introduce the arrays and its usage
Upload your documents to download.
An array is a group of contiguous or related data items that share a common name. For
instance, we can define an array name marks to represent a set of marks of a class of students.
A particular value is indicated by writing a number called index number or subscript in brackets
after the array name.
Syntax:
Examples:
int[] counter;documents.
//declare int array reference
float[] x,y;
Creation:
Start your 30 day free trial
After declaring an array, we need to create it in the memory. C# allows us to create arrays
using new operator only.
Syntax:
Examples:
Initialization:
Initialization of Arrays
Syntax:
arrayname[subscript] = value;
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
34
Example:
number[1] = 19;
Upload
number[2] = 0;
your documents to download.
number[3] = 22;
number[4] = 18;
Upload to Download
Note that C# creates arrays starting with a subscript of 0 and ends with a value one less than
the size specified.
OR
We can also initialize arrays automatically in the same way as the ordinary variables when
they are declared, as shown below:
Example:
Code:
using System;
namespace ArrayApplication {
class MyArray {
int i,j;
n[ i ] = i + 100;
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
35
Console.ReadKey();
Upload to Download
}
Example:
Element[0] = 100
Element[1] = 101
Element[2] = 102
OR
Element[3] = 103
Element[5] = 105
Element[6] = 106
documents.
Element[7] = 107
Element[8] = 108
int [ , , ] m;
Two-Dimensional Arrays
The simplest form of the multidimensional array is the 2-dimensional array. A 2-dimensional
array is a list of one-dimensional arrays.
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
36
A 2-dimensional array can be thought of as a table, which has x number of rows and y number
Upload to Download
Thus, every element in the array a is identified by an element name of the form a[ i , j ], where
a is the name of the array, and i and j are the subscripts that uniquely identify each element
in array a.
Become a Scribd
{0, 1, 2, 3} , member toindexed
/* initializers for row readby 0 */and download full
};
Syntax:
Code:
using System;
namespace ArrayApplication {
class MyArray {
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
37
int i, j;
Upload Console.WriteLine("a[{0},{1}]
your documents to download.
= {2}", i, j, a[i,j]);
Upload to Download
Console.ReadKey();
Output:
a[0,0]: 0
OR
a[0,1]: 0
a[1,1]: 2
a[2,0]: 2
documents.
a[2,1]: 4
a[3,0]: 3
a[4,0]: 4
a[4,1]: 8
A Jagged array is an array of arrays. You can declare a jagged array named scores of
type int as −
Declaring an array, does not create the array in memory. To create the above arra y −
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
38
namespace ArrayApplication {
OR
class MyArray {
Become a Scribdint[][]
member toint[]{0,0},new
a = new int[][]{new read and int[]{1,2},download full
documents.
new int[]{2,4},new int[]{ 3, 6 }, new int[]{ 4, 8 } };
int i, j;
Console.ReadKey();
Output:
a[0][0]: 0
a[0][1]: 0
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
39
a[1][0]: 1
a[1][1]: 2
a[2][1]: 4
Upload a[3][0]:
your 3
documents to download.
a[3][1]: 6
a[4][0]: 4
Upload to Download
a[4][1]: 8
2. Parma Array
At times, while declaring a method, you are not sure of the number of arguments
passed as a parameter. C# param arrays (or parameter arrays) come into help at such
times.
Code:
using System;
OR
namespace ArrayApplication {
class ParamArray {
public int AddElements(params int[] arr) {
Become a Scribd member to read and download full int sum = 0;
documents.
foreach (int i in arr) {
sum += i;
}
return sum;
3. Array Class
The Array class is the base class for all the arrays in C#. It is defined in the System
namespace. The Array class provides various properties and methods to work with
arrays.
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
40
Upload
1
your documents to download.
IsFixedSize
Gets a value indicating whether the Array has a fixed size.
2 IsReadOnly
Gets a value indicating whether the Array is read-only.
3 Length
Upload to Download
Gets a 32-bit integer that represents the total number of elements in all
the dimensions of the Array.
4 LongLength
Gets a 64-bit integer that represents the total number of elements in all
the dimensions of the Array.
5 Rank
OR
Gets the rank (number of dimensions) of the Array.
Sets a documents.
1 Clear
range of elements in the Array to zero, to false, or to null,
depending on the element type.
2 Copy(Array, Array, Int32)
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
41
Upload to Download
Sorts the elements in an entire one-dimensional Array using the
IComparable implementation of each element of the Array.
14 ToString
Returns a string that represents the current object. (Inherited from
Object.)
Code:
using System; OR
namespace ArrayApplication {
class MyArray {
Become a Scribd member to read and download full static void Main(string[] args) {
int[] list = { 34, 72, 13, 44, 25, 30, 10 };
int[] temp = list;
documents. Console.Write("Original Array: ");
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
42
Console.WriteLine();
Console.ReadKey();
Upload Original
your documents to download.
Output:
Array: 34 72 13 44 25 30 10
Reversed Array: 10 30 25 44 13 72 34
Sorted Array: 10 13 25 30 34 44 72
Upload to Download
OR
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
43
Chapter: 4
String
You're Reading a Preview
4.1 Introduce the strings, its usages and functions
Upload your documents to download.
C# supports a predefined reference type known as string. We can use string to declare string
type objects. Remember, when we declare a string using string type, we are in fact declaring
the object to be of type System.String, one of the built-in types provided by the .NET
Upload to Download
Framework. A string type is therefore a System.String type as well.
Usage of string
Most programming languages have a data type called a string, which is used for data values
that are made up of ordered sequences of characters, such as "hello world". A string can
contain any sequence of characters, visible or invisible, and characters may be repeated. The
main usage of string is to store a sequence of characters together in a variable.
OR
4.2 Demonstrate the creation of a string object
BecomeWeacanScribd member to read and download full
create immutable strings using string or String objects in a number of ways.
namespace CsharpString {
class Test {
// create string
// print string
Console.WriteLine(str1);
Console.WriteLine(str2);
Console.ReadLine();
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
44
Output:
Microsoft
Upload to Download
4.3 Demonstrate the methods of string class and deduce its
usages
4.4 Introduce to string functions and examine the usage of
functions
String objects are immutable, meaning that we cannot modify the characters contained in
OR
them. However, since the string is an alias for the predefined System.String class in the
Common Language Runtime (CLR), there are many built-in operations available that work with
strings. All operations produce a modified version of the string rather than modifying the
Become a Scribd member to read and download full
string on which the method is called.
documents.
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
45
{ OR
string str = "Hello World";
Console.WriteLine(str.Length);
Console.WriteLine(str.ToLower());
documents.
Console.WriteLine(str.Trim());
Console.WriteLine(str.Replace("World", "C#"));
Console.WriteLine(word);
Console.WriteLine(str.IndexOf("World"));
Console.WriteLine(str.LastIndexOf("World"));
Console.WriteLine(str.Insert(6, "C#"));
Console.WriteLine(str.Remove(6, 3));
Console.WriteLine(str.PadLeft(20));
Console.WriteLine(str.PadRight(20));
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
46
Console.WriteLine(ch);
11HELLO WORLD
hello world
Hello World
Hello C#World
Hel
OR
W
H W
Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta
Read ng om he Keyboa d
Upload
O
your
m
documents
m w w
to download
m W w
OR
N W m M A D A G
Chapter 5
Structure
You're Reading a Preview
51 n roduc on o s ruc ure s ea ures and s necess es
Upload your documents
m
to download
A w
m m m w m m
w
m
m m m
Fea u e o S u u e
OR
m m
Become a Scr
A bd member
m m mto read and download full
m m
Ne e
documents
e o S u u e
m
m m
m m
N W m M A D A G
A gn ng Va u o M mb
OR
M m m w
m m m
documents
N W m M A D A G
mm
m
OR
mm
N W m M A D A G
N A
mm
OR
w w
N W m M A D A G
W A w
O
OR
A
N W m M A D A G
Chapter 6
Po nter
You're Reading a Preview
61 n roduce he po n ers s ea ures and s app ca ons
Upload
A
your
w
documents to download
m m m m
m
Up oad to Down oad
m
OR
m
m U A
&
Fea u e o Po n e
m m
m w m w
m m
N W m M A D A G
M m w
m m w
m m m
Upload
App a on o your
Po n e documents to download
m
m
D advan age
U Start your 30 day free tr a m m
D m O w w
m m
w m
w m m m
6 3 Demons ra e he access o da a va ue us ng po n er
m U A
N W m M A D A G
W D
W A
OR
D
documents
A
m U A
N W m M A D A G
&
w
W
OR w
W A w
A w
m U A
N W m M A D A G
OR
O
A
documents
V
N W m M A D A G
Chapter 7
Fea u e o Da aba e
M m m D
D O
m OR
m
D
U O Q
M U A
Become a Scr
D bd member
M to read and download full
H D
Ne e y o Da aba e documents
n P og amm ng Env onmen
D m
D
Start your 30 day free tr a w
D m m
D D
D D m m
D D D w w
N W m M A D A G
w D w w
Upload your documents
m to download
wm
m
D m D m w
m w w
m
Up oad to Down oad m w w
O m m
m m w N
OR
m
mD
Become aDScr bd member to read and download full
M M documents
M
D m
w
m
U D w
@ D m m U D m w m
w
N W m M A D A G
M M
M
N W m M A D A G
D m
m
U D w
m w
mm m
OR
w
w
m m
m
w
w
G V G V
N W m M A D A G
m D
w m
m OR
mD
m D O
M M
M
documents
D m
w
m
U D w
@ D D O G D m U
D w
N W m M A D A G
mm m
m OR
mm w
Q
mm documents
mm w mm
DM m
m D
N W m M A D A G
39 pages
PD 100% 2
C Sharp Fu Mater a
150 pages
6 pages
PD 100% 2
Programm ng W th ANS and Turbo C
545150379
2 pages
45 pages
21 pages
221 pages
F om Eve and
C Programm ng Core Concepts and
Techn ques
W am Sm th
36 pages
PD 100% 1
C++ Report
26 pages
10 pages
46 pages
Show more
About Support
About Scr bd nc He p / FAQ
Contact us Soc a
nstagram
Lega
Facebook
Terms
P nterest
Pr vacy
Copyr ght
Cook e Preferences
Do not se or share my
persona nformat on
We and our 10 AB TCF partners store and access
Get our
nformat free
on on yourapps
dev ce for the fo ow ng purposes store
and/or access nformat on on a dev ce advert s ng and
content measurement aud ence research and serv ces
deve opment persona sed advert s ng and persona sed
content Persona data may be processed to do the
fo ow ng use prec se geo ocat on data and act ve y scan
Documents
dev ce character st cs for dent ficat on Our th rd party AB
TCF partners may store and access nformat on on your
Language Eng sh
dev ce such as P address and dev ce character st cs Our
Copyr
AB TCF ght © 2025
Partners mayScr bd nc th s persona data on the
process
bas s of eg t mate nterest or w th your consent You may
change or w thdraw your preferences at any t me by c ck ng
on the cook e con or nk however as a consequence you
may not see re evant ads or persona zed content
Our webs te may use these cook es to
Measure the aud ence of the advert s ng on our
webs te w thout profi ng
D sp ay persona zed ads based on your nav gat on
and your profi e
Persona ze our ed tor a content based on your
nav gat on
A ow you to share content on soc a networks or
p atforms present on our webs te
Send you advert s ng based on your ocat on
Pr vacy Po cy
Th rd Part es
Accept A