0% found this document useful (0 votes)
16 views1 page

Www Scribd

The document provides an introduction to C#, a modern, object-oriented programming language developed by Microsoft for the .NET framework. It outlines C#'s features, such as simplicity, compatibility, and versioning, as well as its applications in various types of software development. Additionally, it discusses the structure of C# programs, including class declarations, the Main method, variables, identifiers, keywords, and data types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views1 page

Www Scribd

The document provides an introduction to C#, a modern, object-oriented programming language developed by Microsoft for the .NET framework. It outlines C#'s features, such as simplicity, compatibility, and versioning, as well as its applications in various types of software development. Additionally, it discusses the structure of C# programs, including class declarations, the Main method, variables, identifiers, keywords, and data types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Search

0 ratings · 610 views · 63 pages


C# Notes in Nepal
Uploaded by Hello World
AI-enhanced description

Document Information
This document introduces C# programming langua…

Download
Download as pdf or txt

Chapter: 1
Introduction to C#.NET

1.1 Introduce C# its Features and Applications

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:

• It is a brand-new language derived from the C/C++ family


• It simplifies and modernizes C++
• It is the only component-oriented language available today
• It is the only language designed for the .NET Framework
• It is a concise, lean and modern language
• It has a lean and consistent syntax

Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta

Ad Down oad to read ad-free

C# Features
The language that is designed for both computing and communication is characterised by

You're Reading a Preview


several key features. They are:

• Simple
• Object Oriented

Upload•
your documents to download.
Compatible
Consistent

• Modern
• Versionable

Simple: Upload to Download


C# simplifies C++ by eliminating irksome operators such as ->,:: and pointers. C# treats
integer and Boolean data types as two entirely different types. This means that the use of
in place of--in if statements will be caught by the compiler.

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.

Consistent Start your 30 day free trial


C# supports a unified type system which eliminates the problem of varying ranges of
integer types. All types are treated as objects and developers can extend the type system
simply and easily.

Modern
C# is called a modern language due to a number of features it supports. It supports

o Automatic garbage collection


o Modern approach to debugging and
o Rich intrinsic model for error handling
o Robust security model
o Decimal data type for financial applications

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

Ad Down oad to read ad-free

keywords. With this support, a programmer can guarantee that his new class library will
maintain binary compatibility with the existing client applications.

You're Reading a Preview


C# Applications
C# is a new language developed exclusively to suit the features of .NET platform. It can be
used for a variety of applications that are supported by the .NET platform:

Upload your documents to download.


Console applications

• Windows applications
• Developing Windows controls
• Developing ASP.NET projects


Creating Web controls
Providing Web services
Upload to Download
Development of C# .NET
Microsoft Chairman Bill Gates, the architect of many innovative and path-breaking software

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,

Start your 30 day free trial


Microsoft introduced C# as a de facto language of the
.NET platform. In fact, they had already used C# to
code key modules of the .NET platform. C# has been
particularly designed to build software components
for .NET and supports key features of .NET natively. They are fairly tightly tied together. In
fact, C# compiler is embedded into .NET as shown in Fig.

1.2 Introduce the structure of C#


An executable C# program may contain a
number coding blocks as shown in Fig. The
documentation section consists of a set of
comments giving the name of the program,
the author, date and other details, which the
programmer (or other users) may like to use
at a later stage. Comments must explain,

o Why and what of classes, and the

Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta

Ad Down oad to read ad-free

o How of algorithms
This would greatly help maintaining the program.

You're Reading a Preview


The using directive section will include all those namespaces that contain classes required by
the application. using directives tell the compiler to look in the namespace specified for these
unresolved classes.

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

declares a class, which is an object-oriented construct. As stated earlier, C# is a true object-


oriented language and therefore, 'everything' must be placed inside a class. class is a keyword
and declares that a new class definition follows. SampleOne is a C# identifier that specifies the
name of the class to be defined.

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.

The Main Method

Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta

Ad Down oad to read ad-free

The third line,

You're Reading a Preview


public static void Main()

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

Upload your documents to download.


have any number of classes but 'only one' class can have the Main method to initiate the
execution.

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).

Become a Scribd member to read and download full


The Output Line
documents.
The only executable statement in the program is ,

System.Console.WriteLine("C# is sharper than C++.")

Start your 30 day free trial


This has a striking resemblance to the output statement of Java and similar to the printf() of C
or cout<< of C++. Since C# is a pure object-oriented language, every method should be part of
an object. The WriteLine method is a static method of the Console class, which is located in
the namespace System. This line prints the string C# is sharper than C++ to the screen. The
method WriteLine always appends a new-line character to the end of the string. This means,
any subsequent output will start on a new line.

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.

1.3 Analyze the variables of C#


A variable is an identifier that denotes a storage location used to store a data value. Unlike
constants that remain unchanged during the execution of a program, a variable may take
different values at different times during the execution of the program. Every variable has a
type that determines what values can be stored in the variable.

Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta

Ad Down oad to read ad-free

A variable name can be chosen by the programmer in a meaningful way so as to reflect what

You're Reading a Preview


it represents in the program. Some examples of variable names are:

o Average
o total_height

Upload your documents to download.


o height
o classStrength

As mentioned earlier, variable names may consist of alphabets, digits and the underscore (__),
subject to the following conditions:

1. They must not begin with a digit. Upload to Download


2. Uppercase and lowercase are distinct. This means that the variable Total is not the
same as total or TOTAL.
3. It should not be a keyword.
4. White space is not allowed.
5. Variable names can be of any length.
OR
Variables are the names of storage locations. After designing suitable variable names, we must
declare them to the compiler. Declaration does three things:

Become a1. Scribd member


It tells the compiler toname
what the variable read
is. and download full
2. It specifies what type of data the variable will hold.

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:

type variable1, variable2, ........., variableN;

Example: int count;

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,

type variableName = value;

Example:

Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta

Ad Down oad to read ad-free

value2 = 26;

float roll_no = 19;

1.4
You're Reading a Preview
Describe the Identifiers of C#

Upload your documents


Identifiers are programmer-designed tokens. They areto
useddownload.
for naming classes, methods,
variables, labels, namespaces, interfaces, etc. C# identifiers enforce the following rules:

o They can have alphabets, digits and underscore characters

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

1.5 Introduce the keywords of C# OR


Keywords are an essential part of a language definition. They implement specific features of
the language. They are reserved, and cannot be used as identifiers except when they are
prefaced by the @ character. Below are the lists all the C# keywords:

Become a Scribd member to read and download full


abstract as base bool
break
char
byte
checked
documents.
case
class
catch
const
continue decimal default delegate
do double else enum
event
finally
Start your 30 day free trial
explicit
fixed
extern
float
false
for
foreach goto if implicit
in in (generic modifier) int interface
internal is lock long
namespace new null object
operator out out (generic modifier) override
params private protected public
readonly ref return sbyte
sealed short sizeof stackalloc
static string struct switch
this throw true try
typeof uint ulong unchecked
unsafe ushort using using static
void volatile while

1.6 Explain data types in C#

Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta

Ad Down oad to read ad-free

Every variable in C# is associated with a data type.

You're Reading a Preview


Data types specify the size and type of values that
can be stored. C# is a language rich in its data types.
The variety available allows the programmer to
select the type appropriate to the needs of the

Upload your documents to download.


application.
The types in C# are primarily divided into three
categories:

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

Become a Scribd member to read and download full


with a numeric value.
Example

documents.
int myNum = 100000;
Console.WriteLine(myNum);

Start your 30 day free trial


Long
The long data type can store whole numbers from -9223372036854775808 to
9223372036854775807. This is used when int is not large enough to store the value. Note
that you should end the value with an "L":
Example
long myNum = 15000000000L;
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

Ad Down oad to read ad-free

Double
It is 64-bit double-precision floating point type. It has 14 – 15 digit Precision. To initialize a

You're Reading a Preview


double variable, use the suffix d or D.
Example:

double d1 = 324.2D;

Upload your documents to download.


Console.WriteLine(d1);

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

char myGrade = 'B';


Become a Scribd member to read and download full
Console.WriteLine(myGrade);

documents.
Strings
The string data type is used to store a sequence of characters (text). String values must be

Start your 30 day free trial


surrounded by double quotes:
Example

string greeting = "Hello World";

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

Ad Down oad to read ad-free

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

You're Reading a Preview


actual value remains in the same memory location. This means that there are two
references to a single value.

User-defined reference types refer to those types which we define using predefined types.

Upload your documents to download.


They include:

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

1.7 State the C# type conversion


Become a Scribd member to read and download full
Type conversion is converting one type of data to another type. It is also known as Type

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.

Start your 30 day free trial


The process of assigning a smaller type to a larger one is known as widening or promotion and
that of assigning a larger type to a smaller one is known as narrowing. Note that narrowing
may result in loss of information.

In C#, there are two basic types of type conversion:

1. Implicit Type Conversions


2. Explicit Type Conversions

1. Implicit Type Conversion


In implicit type conversion, the C# compiler automatically converts one type to another.
Generally, smaller types like int (having less memory size) are automatically converted to
larger types like double (having larger memory size).

char -> int -> long -> float -> double

An implicit conversion is also known as automatic type conversion.

Example:

Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta

Ad Down oad to read ad-free

11

short sa = 26;

int ah = sa; // implicit conversion

You're Reading a Preview


float sk = 45232.23;

double as = sk; // implicit conversion

Upload your documents to download.


Code:

using System;
namespace MyApplication
{
class Program
{
Upload to Download
static void Main(string[] args)
{
int numInt = 500;

// get type of numInt


Type n = numInt.GetType();
OR
// Implicit Conversion
double numDouble = numInt;

// get type of numDouble


Become a Scribd
Type n1member to read and download full
= numDouble.GetType();

documents.
// Value before conversion
Console.WriteLine("numInt value: " + numInt);
Console.WriteLine("numInt Type: " + n);

// Value after conversion

Start your 30 day free trial


Console.WriteLine("numDouble value: " + numDouble);
Console.WriteLine("numDouble Type: " + n1);
Console.ReadLine();
}
}
}

Output:

numInt value: 500

numInt Type: System.Int32

numDouble value: 500

numDouble Type: System.Double

2. Explicit Type Conversion

Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta

Ad Down oad to read ad-free

12

In explicit type conversion, we explicitly convert one type to another.


Generally, larger types like double (having large memory size) are converted to smaller

You're Reading a Preview


types like int (having small memory size).

double -> float -> long -> int -> char

Explicit casting must be done manually by placing the type in parentheses in front of the

Upload your documents to download.


value. We can explicitly carry out such conversions using the 'cast' operator. The process
is known as casting and is done as follows:

type variable1 = (type) variable2;

Example:

int m = 50; Upload to Download


byte n = (byte) m;
long x = 1234L;
int y = (int) x;
float f = 50.OF;
long y = (long) f;
OR
Code:

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);

// Value before conversion


Console.WriteLine("Converted int Value: "+numInt);
Console.ReadLine();
}
}
}
Output:
Original double value: 1.23
Converted int value: 1

Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta

Ad Down oad to read ad-free

13

1.8 Introduce to C# operators

You're Reading a Preview


An operator is a symbol that tells the computer to perform certain mathematical or logical
manipulations. Operators are used in programs to manipulate data and variables. They usually
form a part of mathematical or logical expressions.

Upload your documents to download.


C# supports a rich set of operators. C# operators can be classified into a number of related
categories as below:

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.

Operator Description Example

Become a+- Scribd member to read and download


Adds two operands
Subtracts second operand from the first
A + B = 30
A B = -10
full –

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

Start your 30 day free trial


-- Decrement operator decreases integer value by one A-- = 9

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

Ad Down oad to read ad-free

14

Console.WriteLine("Line 4 - Value of c is {0}", c);


c = a % b;

You're Reading a Preview


Console.WriteLine("Line 5 - Value of c is {0}", c);
c = a++;
Console.WriteLine("Line 6 - Value of c is {0}", c);
c = a--;
Console.WriteLine("Line 7 - Value of c is {0}", c);
Upload
}
your documents to download. Console.ReadLine();

}
}

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

Ad Down oad to read ad-free

15

if (a == b)
{

You're Reading a Preview


Console.WriteLine("Line 1 - a is equal to b");
}
else
{
Console.WriteLine("Line 1 - a is not equal to b");
Upload your documents to download. }

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
{

Become a Scribd member to read and download full }


Console.WriteLine("Line 3 - a is not greater than b");

documents.
/* Lets change value of a and b */
a = 5;
b = 20;

Start your 30 day free trial if (a <= b)


{
Console.WriteLine("Line 4 - a is either less than or equal to b");
}

if (b >= a)
{
Console.WriteLine("Line 5-b is either greater than or equal to b");
}
}
}

Output:

Line 1 - a is not equal to b


Line 2 - a is not less than b
Line 3 - a is greater than b
Line 4 - a is either less than or equal to b
Line 5 - b is either greater than or equal to b

Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta

Ad Down oad to read ad-free

16

C. Logical Operator

You're Reading a Preview


Logical operators are used to perform logical operation such as and, or. Logical operators
operates on boolean expressions (true and false) and returns boolean values. Logical
operators are used in decision making and loops.

Operator Description Example

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;

Become a Scribd member to read and download full


bool b = 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
}

/* lets change the value of a and b */


a = false;
b = true;

if (a && b) {
Console.WriteLine("Line 3 - Condition is true");
} else {
Console.WriteLine("Line 3 - Condition is not true");
}

if (!(a && b)) {


Console.WriteLine("Line 4 - Condition is true");
}
Console.ReadLine();
}
}
}

Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta

Ad Down oad to read ad-free

17

Output:

Line 1 - Condition is true

You're Reading a Preview


Line 2 - Condition is true
Line 3 - Condition is not true
Line 4 - Condition is true

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, '='.

Operator Description Example

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

Become a%=Scribd member to read and


C %= A download full
operand
It takes modulus using two operands and
is equivalent to C = C % A
assign the result to left operand
<<=
>>=
documents. C >>= 2 is same as C = C >> 2
Left shift AND assignment operator
C <<= 2 is same as C = C << 2
Right shift AND assignment operator
&= Bitwise AND assignment operator C &= 2 is same as C = C & 2
^= bitwise exclusive OR and assignment C ^= 2 is same as C = C ^ 2

|= Start your 30 day free trial


operator
bitwise inclusive OR and assignment C |= 2 is same as C = C | 2
operator

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

Ad Down oad to read ad-free

18

c -= a;

You're Reading a Preview


Console.WriteLine("Line 3 - -= Value of c = {0}", c);

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;

Become a Scribd member to read and download full


Console.WriteLine("Line 10 - ^= Value of c = {0}", c);

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

Ad Down oad to read ad-free

19

E. Conditional Operator
Ternary operator is a Conditional operator in C#. It takes three arguments and evaluates

You're Reading a Preview


a Boolean expression.

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

F. Bitwise Operator Start your 30 day free trial


Bitwise and bit shift operators are used to perform bit manipulation operations.

Operator Description Example


& Binary AND Operator copies a bit to the (A & B) = 12, which is 0000 1100
result if it exists in both operands.
| Binary OR Operator copies a bit if it exists (A | B) = 61, which is 0011 1101
in either operand.
^ Binary XOR Operator copies the bit if it is (A ^ B) = 49, which is 0011 0001
set in one operand but not both.
~ Binary Ones Complement Operator is (~A ) = -61, which is 1100 0011 in 2's
unary and has the effect of 'flipping' bits. complement due to a signed binary
number.
<< Binary Left Shift Operator. The left A << 2 = 240, which is 1111 0000
operands value is moved left by the
number of bits specified by the right
operand.
>> Binary Right Shift Operator. The left A >> 2 = 15, which is 0000 1111
operands value is moved right by the

Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta

Ad Down oad to read ad-free

20

number of bits specified by the right


operand.

You're Reading a Preview


Code:
using System;
namespace OperatorsAppl
{
Upload your documents to download.
class Program
{
static void Main(string[] args)
{
int a = 60; /* 60 = 0011 1100 */
int b = 13;
int c = 0;
Upload to Download
/* 13 = 0000 1101 */

c = a & b; /* 12 = 0000 1100 */


Console.WriteLine("Line 1 - Value of c is {0}", c);

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);

c = a >> 2; /* 15 = 0000 1111 */

Start your 30 day free trial


Console.WriteLine("Line 6 - Value of c is {0}", c);
Console.ReadLine();
}
}
}

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

Ad Down oad to read ad-free

21

G. Miscellaneous Operator

You're Reading a Preview


Operator Description Example
sizeof() Returns the size of a data type. sizeof(int), returns 4.
typeof() Returns the type of a class. typeof(StreamReader);
& Returns the address of an variable. &a; returns actual address of the
variable.
Upload
* your
Pointer to adocuments
variable. to download. *a; creates pointer named 'a' to a
variable.
?: Conditional Expression If Condition is true ? Then value X :
Otherwise value Y
is Determines whether an object is of a If( Ford is Car) // checks if Ford is an

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 {

static void Main(string[] args) {

Become a Scribd member to read and download full


/* example of sizeof operator */
Console.WriteLine("The size of int is {0}", sizeof(int));
Console.WriteLine("The size of short is {0}", sizeof(short));

documents. Console.WriteLine("The size of double is {0}", sizeof(double));

/* example of ternary operator */


int a, b;
a = 10;
Start your 30 day free trial
b = (a == 1) ? 20 : 30;
Console.WriteLine("Value of b is {0}", b);

b = (a == 10) ? 20 : 30;
Console.WriteLine("Value of b is {0}", b);
Console.ReadLine();
}
}
}

Output:

The size of int is 4


The size of short is 2
The size of double is 8
Value of b is 30
Value of b is 20

Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta

Ad Down oad to read ad-free

22

Chapter: 2

You're Reading a Preview


Control Statements

2.1 Introduce the control statements


Upload your documents to download.
When a program breaks the sequential flow and jumps to another part of the code, it is called
branching. When the branching is based on a particular condition, it is known as conditional
branching. If branching takes place without any decision, it is known as unconditional

Upload to Download
branching.

C# language possesses such decision-making capabilities and supports the following


statements known as control or decision-making statements.

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;

public class GFG {

public static void Main(string[] args)


{
string name = "Sam";
if (name == "Sam") {
Console.WriteLine("If statement executed.");
}
}
}

Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta

Ad Down oad to read ad-free

23

Output:
If statement executed.

You're Reading a Preview


2. if else statement

Upload your documents to download.


The if statement evaluates the code if the condition is true but what if the condition is not
true, here comes the else statement. It tells the code what to do when the if condition is
false.

Syntax:
if(condition)
{
Upload to Download
// code if condition is true
}
else
{

}
// code if condition is false
OR
Code:
using System;

Become a Scribd member to read and download full


public class GFG {

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.

3. if else if else statement


The if-else-if ladder statement executes one condition from multiple statements. The
execution starts from top and checked for each if condition. The statement of if block will
be executed which evaluates to be true. If none of the if condition evaluates to be true
then the last else block is evaluated.
Syntax:

if(condition1)

Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta

Ad Down oad to read ad-free

24

{
// code to be executed if condition1 is true

You're Reading a Preview


}
else if(condition2)
{
// code to be executed if condition2 is true

Upload your documents to download.


}
else if(condition3)
{
// code to be executed if condition3 is true
}
...
else Upload to Download
{
// code to be executed if all the conditions are false
}

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)

Start your 30 day free trial


Console.WriteLine("i is 10");
else if (i == 15)
Console.WriteLine("i is 15");
else if (i == 20)
Console.WriteLine("i is 20");
else
Console.WriteLine("i is not present");
}
}

Output:
i is 20

Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta

Ad Down oad to read ad-free

25

2.3 Demonstrate the switch statement and its functions

You're Reading a Preview


4. Switch Statement
A switch statement allows a variable to be tested for equality against a list of values. There
is default case (optional) at the end of switch, if none of the case matches then default
case is executed.

Upload your documents to download.


It provides an efficient way to transfer the execution to different parts of a code based on
the value of the expression.

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;

Start your 30 day free trial


public class GFG
{
public static void Main(String[] args)
{
int number = 30;
switch(number)
{
case 10: Console.WriteLine("case 10");
break;
case 20: Console.WriteLine("case 20");
break;
case 30: Console.WriteLine("case 30");
break;
default: Console.WriteLine("None matches");
break;
}
}
}

Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta

Ad Down oad to read ad-free

26

Output:

You're Reading a Preview


case 30

2.4 Illustrate the for loop and deduce its usage


Upload
The process of your
repeatedly documents to download.
executing a block of statements is known as looping. The
statements in the block may be executed any number of times, from zero to an infinite
number. Loops are of two types:

A. Entry Control Loop:


Upload to Download
The loops in which condition to be tested is present in beginning of loop body are known
as Entry Controlled Loops.
1. For Loop
2. While Loop
3. For Each Loop

B. Exit Control Loop OR


The loops in which the testing condition is present at the end of loop body are termed as
Exit Controlled Loops.
4. Do while Loop

Become a Scribd member to read and download full


1. For Loop
documents.
for loop has similar functionality as while loop but with different syntax. The loop variable
initialization, condition to be tested, and increment/decrement of the loop variable is
done in one line in for loop thereby providing a shorter, easy to debug structure of looping.

-
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.

- Increment / Decrement: The loop variable is incremented/decremented


according to the requirement and the control then shifts to the testing condition
again.

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

Ad Down oad to read ad-free

27

class forLoopDemo
{

You're Reading a Preview


public static void Main()
{
// for loop begins when x=1
// and runs till x <=4

Upload your documents to download.


for (int x = 1; x <= 4; x++)
Console.WriteLine("Sam {0}", x);
}
}

Output:
Sam 1 Upload to Download
Sam 2
Sam 3
Sam 4

2.5 Illustrate the do while loop and deduce its usage.


OR
2. While Loop:
The test condition is given in the beginning of the loop and all statements are executed
till the given boolean condition satisfies when the condition becomes false, the control

Become a Scribd member to read and download full


will be out from the while loop.

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;

// Exit when x becomes greater than 4


while (x <= 4)
{
Console.WriteLine("Sam {0}", x);
x++;
}
}
}

Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta

Ad Down oad to read ad-free

28

Output:
Sam 1

You're Reading a Preview


Sam 2
Sam 3
Sam 4

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 {

Become a Scribd member to read and download full


// Main Method

documents.
static public void Main()

Console.WriteLine("Print array:");

Start your 30 day free trial


int[] a_array = new int[] { 1, 2, 3, 4, 5, 6, 7 };

foreach(int items in a_array)

Console.WriteLine(items);

Output:

Print array:

Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta

Ad Down oad to read ad-free

29

You're Reading a Preview


6

Upload your documents to download.


2.6 Illustrate the while loop and deduce its usage
1. Do while Loop
Upload to Download
do while loop is similar to while loop with the only difference that it checks the condition
after executing the statements, i.e. it will execute the loop body one time for sure because
it checks the condition after executing the statements.

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

2.7 Classify loop control statements and compare its feature

Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta

Ad Down oad to read ad-free

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.

You're Reading a Preview


C# provides the following control statements.

2. break statement
Terminates the loop or switch statement and transfers execution to the statement

Upload your documents to download.


immediately following the loop or switch.

Syntax:
break;
Code:
using System;
namespace Loops {
Upload to Download
class Program {
static void Main(string[] args) {
/* local variable definition */
int a = 10;

/* while loop execution */


OR
while (a < 20) {
Console.WriteLine("value of a: {0}", a);
a++;

Become a Scribd member to read and download full


if (a > 15) {

documents.
/* terminate the loop using break statement */
break;
}
}

Start your 30 day free trial


Console.ReadLine();
}
}
}

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

Ad Down oad to read ad-free

31

continue;

You're Reading a Preview


Code:
using System;
namespace Loops {
class Program {

Upload your documents to download.


static void Main(string[] args) {
/* local variable definition */
int a = 10;

/* 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

Start your 30 day free trial


value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19

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.

Currently, it is avoided to use goto statement in C# because it makes the program


complex.

Code:

Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta

Ad Down oad to read ad-free

32

using System;
public class GotoExample

You're Reading a Preview


{
public static void Main(string[] args)
{
ineligible:

Upload your documents to download.


Console.WriteLine("You are not eligible to vote!");

Console.WriteLine("Enter your age:\n");


int age = Convert.ToInt32(Console.ReadLine());
if (age < 18){

} Upload to Download
goto ineligible;

else
{
Console.WriteLine("You are eligible to vote!");
}

}
}
OR
Output:
You are not eligible to vote!
Enter your age:

Become a Scribd member to read and download full


11
You are not eligible to vote!
Enter your age:
documents.
5
You are not eligible to vote!
Enter your age:
26

Start your 30 day free trial


You are eligible to vote!

Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta

Ad Down oad to read ad-free

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.

Usage: Upload to Download


You can store multiple values in a single variable by using array in C#.

3.2 Demonstrate the declaration and initialization of array


OR
Declaration:

Syntax:

Become a Scribd member to read and download full


type[] arrayname;

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:

arrayname new type[size];

Examples:

number new int[5]; //create a 5 element int array

average new float[10]; // create a 10 element float array

Initialization:

Initialization of Arrays

This process is known as initialization.

Syntax:

arrayname[subscript] = value;

Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta

Ad Down oad to read ad-free

34

Example:

You're Reading a Preview


number[0] = 26;

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:

type [] arrayname = {list of values);

Example:

Become a Scribd member to read and download full


int[] number [35, 40, 20, 57, 19};

3.3 Illustrate the datadocuments.access from an array


An element is accessed by indexing the array name. This is done by placing the index of the

Start your 30 day free trial


element within square brackets after the name of the array. For example,

double salary = balance[9];

Code:

using System;

namespace ArrayApplication {

class MyArray {

static void Main(string[] args) {

int [] n = new int[10]; /* n is an array of 10 integers */

int i,j;

/* initialize elements of array n */

for ( i = 0; i < 10; i++ ) {

n[ i ] = i + 100;

Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta

Ad Down oad to read ad-free

35

/* output each array element's value */

You're Reading a Preview for (j = 0; j < 10; j++ ) {

Console.WriteLine("Element[{0}] = {1}", j, n[j]);

Upload your documents to download.


}

Console.ReadKey();

Upload to Download
}

Example:

Element[0] = 100

Element[1] = 101

Element[2] = 102
OR
Element[3] = 103

Become a Scribd member to read and download full


Element[4] = 104

Element[5] = 105

Element[6] = 106
documents.
Element[7] = 107

Element[8] = 108

Start your 30 day free trial


Element[9] = 109

3.4 Introduce to multidimensional arrays


C# allows multidimensional arrays. Multi-dimensional arrays are also called rectangular array.
You can declare a 2- dimensional array of strings as −

string [,] names;

or, a 3-dimensional array of int variables as −

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

Ad Down oad to read ad-free

36

A 2-dimensional array can be thought of as a table, which has x number of rows and y number

You're Reading a Preview


of columns. Following is a 2-dimensional array, which contains 3 rows and 4 columns:

Upload your documents to download.

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.

Initializing Two-Dimensional Arrays


Multidimensional arrays may be initialized by specifying bracketed values for each row. The
OR
Following array is with 3 rows and each row has 4 columns.

int [,] a = new int [3,4] {

Become a Scribd
{0, 1, 2, 3} , member toindexed
/* initializers for row readby 0 */and download full

{4, 5, 6, 7} , /* initializers for row indexed by 1 */


documents.
{8, 9, 10, 11} /* initializers for row indexed by 2 */

};

Start your 30 day free trial


Accessing Two-Dimensional Array Elements
An element in 2-dimensional array is accessed by using the subscripts. That is, row index and
column index of the array.

Syntax:

int val = a[2,3];

Code:

using System;

namespace ArrayApplication {

class MyArray {

static void Main(string[] args) {

/* an array with 5 rows and 2 columns*/

int[,] a = new int[5, 2] {{0,0}, {1,2}, {2,4}, {3,6}, {4,8} };

Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta

Ad Down oad to read ad-free

37

int i, j;

/* output each array element's value */

You're Reading a Preview for (i = 0; i < 5; i++) {

for (j = 0; j < 2; 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

Become a Scribd member to read and download full


a[1,0]: 1

a[1,1]: 2

a[2,0]: 2
documents.
a[2,1]: 4

a[3,0]: 3

Start your 30 day free trial


a[3,1]: 6

a[4,0]: 4

a[4,1]: 8

3.5 Compare and deduce the applications of jagged arrays,


param arrays, and array class
1. Jagged Arrays

A Jagged array is an array of arrays. You can declare a jagged array named scores of
type int as −

int [][] scores;

Declaring an array, does not create the array in memory. To create the above arra y −

int[][] scores = new int[5][];

Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta

Ad Down oad to read ad-free

38

for (int i = 0; i < scores.Length; i++) {

You're Reading a Preview scores[i] = new int[4];

Upload your documents to download.


You can initialize a jagged array as −

int[][] scores = new int[2][]{new int[]{92,93,94},new int[]{85,66,87,88}};

Where, scores is an array of two arrays of integers - scores[0] is an array of 3 integers


and scores[1] is an array of 4 integers.

Example: Upload to Download


using System;

namespace ArrayApplication {

OR
class MyArray {

static void Main(string[] args) {

/* a jagged array of 5 array of integers*/

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;

/* output each array element's value */

Start your 30 day free trial for (i = 0; i < 5; i++) {

for (j = 0; j < 2; j++) {

Console.WriteLine("a[{0}][{1}] = {2}", i, j, a[i][j]);

Console.ReadKey();

Output:

a[0][0]: 0

a[0][1]: 0

Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta

Ad Down oad to read ad-free

39

a[1][0]: 1

a[1][1]: 2

You're Reading a Preview a[2][0]: 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;

Start your 30 day free trial


}
}
class TestClass {
static void Main(string[] args) {
ParamArray app = new ParamArray();
int sum = app.AddElements(512, 720, 250, 567, 889);

Console.WriteLine("The sum is: {0}", sum);


Console.ReadKey();
}
}
}
Output:
The sum is: 2938

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

Ad Down oad to read ad-free

40

Properties of the Array Class

You're Reading a Preview


The following table describes some of the most commonly used properties of the
Array class –

Sr.No. Property & description

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.

Methods of the Array Class


The following table describes some of the most commonly used methods of the Array
class −

Become a Scribd member to read and download full


Sr.No. Methods & Description

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)

Start your 30 day free trial


Copies a range of elements from an Array starting at the first element
and pastes them into another Array starting at the first element. The
length is specified as a 32-bit integer.
3 CopyTo(Array, Int32)
Copies all the elements of the current one-dimensional Array to the
specified one-dimensional Array starting at the specified destination
Array index. The index is specified as a 32-bit integer.
4 GetLength
Gets a 32-bit integer that represents the number of elements in the
specified dimension of the Array.
5 GetLongLength
Gets a 64-bit integer that represents the number of elements in the
specified dimension of the Array.
6 GetLowerBound
Gets the lower bound of the specified dimension in the Array.
7 GetType
Gets the Type of the current instance. (Inherited from Object.)
8 GetUpperBound
Gets the upper bound of the specified dimension in the Array.
9 GetValue(Int32)

Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta

Ad Down oad to read ad-free

41

Gets the value at the specified position in the one-dimensional Array.


The index is specified as a 32-bit integer.

You're Reading a Preview


10 IndexOf(Array, Object)
Searches for the specified object and returns the index of the first
occurrence within the entire one-dimensional Array.
11 Reverse(Array)
Reverses the sequence of the elements in the entire one-dimensional
Upload
12
your documents to download. Array.
SetValue(Object, Int32)
Sets a value to the element at the specified position in the one-
dimensional Array. The index is specified as a 32-bit integer.
13 Sort(Array)

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: ");

foreach (int i in list) {


Console.Write(i + " ");

Start your 30 day free trial }


Console.WriteLine();

// reverse the array


Array.Reverse(temp);
Console.Write("Reversed Array: ");

foreach (int i in temp) {


Console.Write(i + " ");
}
Console.WriteLine();

//sort the array


Array.Sort(list);
Console.Write("Sorted Array: ");

foreach (int i in list) {


Console.Write(i + " ");
}

Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta

Ad Down oad to read ad-free

42

Console.WriteLine();
Console.ReadKey();

You're Reading a Preview


}
}
}

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

Become a Scribd member to read and download full


documents.

Start your 30 day free trial

Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta

Ad Down oad to read ad-free

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.

Assigning string literals


Concatenating two objects



documents.
Copying from one object to another

• Reading from the keyboard


• Using ToString method.

Code: Start your 30 day free trial


using System;

namespace CsharpString {

class Test {

public static void Main(string [] args) {

// create string

string str1 = "C# Programming";

string str2 = "Microsoft";

// print string

Console.WriteLine(str1);

Console.WriteLine(str2);

Console.ReadLine();

Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta

Ad Down oad to read ad-free

44

You're Reading a Preview


}

Output:

Upload your documents to download.


C# Programming

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.

Start your 30 day free trial

Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta

Ad Down oad to read ad-free

45

You're Reading a Preview


Upload your documents to download.
Code:

using System; Upload to Download


class HelloWorld

static void Main()

{ OR
string str = "Hello World";

Console.WriteLine(str.Length);

Become a Scribd member to read and download full


Console.WriteLine(str.ToUpper());

Console.WriteLine(str.ToLower());
documents.
Console.WriteLine(str.Trim());

Console.WriteLine(str.Replace("World", "C#"));

Start your 30 day free trial


Console.WriteLine(str.Substring(6, 5));

string[] words = str.Split(' ');

foreach (string word in words)

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

Ad Down oad to read ad-free

46

char[] chars = str.ToCharArray();

foreach (char ch in chars)

You're Reading a Preview {

Console.WriteLine(ch);

Upload your documents to download


}

Up oad to Down oad


Output:

11HELLO WORLD

hello world

Hello World

Hello C#World

Hel
OR
W

Become a Scr bd member to read and download full


H W
documents
H

H W

H Start your 30 day free tr a


W

Notes Written By: Sanjay Kurmi (MCA), Digitalized By: Ashish Gupta

Ad Down oad to read ad-free

Read ng om he Keyboa d

You're Reading a Preview m

Upload
O
your
m
documents
m w w
to download
m W w

Up oad to Down oad

OR

Become a Scr bd member to read and download full


documents

Start your 30 day free tr a

N W m M A D A G

Ad Down oad to read ad-free

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

o Up oad to Down oad m m


o m
o

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

Start your 30 day free tr a


m m
w w m
m w m m m w m A
m

5 2 Demons ra on o De n ng o s ruc ure and s usage


w m m
w

m m
m m

N W m M A D A G

Ad Down oad to read ad-free

You're Reading a Preview m

Upload your documents to download


m w w

Up oad to Down oad


m m m

A gn ng Va u o M mb
OR
M m m w

m m m

Become a Scrmbd member to read and download full


m

documents

Start your 30 day free tr a


m

N W m M A D A G

Ad Down oad to read ad-free

You're Reading a Preview


mm

Upload your documents


A
to download N

mm

Up oad to Down oad


m

m
OR

Become a Scr bdW member to read and download full


W
documents
W

Start your 30 day free tr a


W

mm

N W m M A D A G

Ad Down oad to read ad-free

N A

mm

You're Reading a Preview


m

Upload your documents


A
to download
m

Up oad to Down oad


5 3 Compare and eva ua e c ass vs s ruc ure and
demons ra e

OR

Become a Scr bd member to read and download full


documents

Start your 30 day free tr a


m

w w

N W m M A D A G

Ad Down oad to read ad-free

W A w

You're Reading a Preview


Upload your documents to download
M

Up oad to Down oad


w

O
OR
A

Become a Scr bd member to read and download full


documents

Start your 30 day free tr a

N W m M A D A G

Ad Down oad to read ad-free

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

Become a Scr bd mmember to read and download full


documents
M

&

Start your 30 day free tr a


W D
W A

Fea u e o Po n e
m m
m w m w
m m

N W m M A D A G

Ad Down oad to read ad-free

M m w
m m w

You're Reading a Preview


w w
m m m

m m m

Upload
App a on o your
Po n e documents to download
m
m

Up oad to Down oad


m
D m m m
m m

6 2 D eren a e be ween advan ages and d sadvan ages o


po n ers OR
Advan age
m m

Become a Scr bd member


w
mto read and
m
m download full
m m
documents
w m
w m m 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

Ad Down oad to read ad-free

You're Reading a Preview


Upload your documents to download
&

W D

Up oad to Down oad


W D

W A

OR

Become a Scr bd member to read and download full


O

D
documents
A

64 us ra e he pass ng o Start your 30 day


po n free
ers as tr a ers
parame o
me hods

m U A

N W m M A D A G

Ad Down oad to read ad-free

You're Reading a Preview


M

Upload your documents to download


w

Up oad to Down oad


&

&

w
W
OR w

W A w

Become a Scr bd member to read and download full


documents

O Start your 30 day free tr a


w

A w

6 5 Demons ra e he access o array e emen s us ng a po n er

m U A

N W m M A D A G

Ad Down oad to read ad-free

You're Reading a Preview


Upload your documents to download
W A

Up oad to Down oad


W V

OR
O

Become a Scr bd member to read and download full


A

A
documents
V

V Start your 30 day free tr a

N W m M A D A G

Ad Down oad to read ad-free

Chapter 7

You're Reading a Preview


Work ng w th database

71 n roduce he da abase s ea ures and necess y n


Upload
programmyour
ng envdocuments
ronmen to download
A m
m m A
m m
Up oad to Down oad
m D M
w m
D M
m
w

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

7 2 Demons ra e he Da abase env ronmen se up and


con gure he requ remen s
w …

N W m M A D A G

Ad Down oad to read ad-free

73 us ra e he connec on o C# program w h da abase

You're Reading a Preview


N w w m m mm O
M Q w w w m
m m

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

Start your 30 day free tr a

D m
w

m
U D w

@ D m m U D m w m
w

N W m M A D A G

Ad Down oad to read ad-free

You're Reading a Preview


O
W O

Upload your documents to download


O

Up oad to Down oad


O

7 4 Demons ra e he Read and wr e opera ons rom he


da abase
Read o m da aba e
OR
m w

Become a Scr bd member


m to read and download full
m
mD documents
m D O

Start your 30 day free tr a


m

M M
M

N W m M A D A G

Ad Down oad to read ad-free

D m

You're Reading a Preview


w

m
U D w

Upload your documents


@ D tom downloadm U
D m w m

Up oad to Down oad


O

m w

mm m
OR
w

Become a Scr bd member


Q mm to read and download full
Q mm
documents
w m m
D N m m m

Start your 30 day free tr a m


m w mm

w
m m
m

w
w

G V G V

N W m M A D A G

Ad Down oad to read ad-free

m D

You're Reading a Preview


Upload
O
your documents to download

Up oad to Down oad


W e o da aba e

w m

m OR
mD

m D O

Become a Scr bd member to read and download full m

M M
M
documents

Start your 30 day free tr a

D m
w

m
U D w

@ D D O G D m U
D w

N W m M A D A G

Ad Down oad to read ad-free

You're Reading a Preview O

Upload your documents to download


m w

mm m

D Up oad to Down oad


A w
mm
D A

m OR
mm w

Become a Scr bd member to read and download full m w mm

Q
mm documents
mm w mm

DM m

Start your 30 day free tr a mm N Q

m D

N W m M A D A G

Share this document

You might also like


PD 100% 13
T24 Java Tra n ng nterna - 20191022

39 pages

PD 100% 2
C Sharp Fu Mater a

150 pages

PD No rat ngs yet


DotNet Notes

6 pages

PD 100% 2
Programm ng W th ANS and Turbo C
545150379

2 pages

PD No rat ngs yet


ntroduc ng C#

45 pages

PD No rat ngs yet


ntroduct on To C-Sharp

21 pages

PD No rat ngs yet


Programm ng n C#

221 pages

F om Eve and
C Programm ng Core Concepts and
Techn ques
W am Sm th

No rat ngs yet

PD No rat ngs yet


C Programm ng V va Q&a

36 pages

PD 100% 1
C++ Report

26 pages

PD No rat ngs yet


C Sharp Presentat on S des

10 pages

PD No rat ngs yet


Ob ect Or ented Programm ng
ntroduct on To C#

46 pages

Show more

Documents  Computers  Programm ng

About Support
About Scr bd nc He p / FAQ

Everand Ebooks & Access b ty


Aud obooks
Purchase he p
S deShare
AdCho ces
Jo n our team

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

Custom ze Your Cho ces

Accept A

Cont nue W thout Accept ng

You might also like