0% found this document useful (0 votes)
6 views

Assignment C#pathak

C sharp assignment

Uploaded by

insanerider888
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Assignment C#pathak

C sharp assignment

Uploaded by

insanerider888
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Q1. Define the Architecture of .NET Framework.

Ans- .Net Framework is a software development platform developed by Microsoft for


building and running Windows applications. The .Net framework consists of developer tools,
programming languages, and libraries to build desktop and web applications. It is also used to
build websites, web services, and games. The .Net framework was meant to create applications,
which would run on the Windows Platform. The first version of the .Net framework was
released in the year 2002.

In short, .Net framework is a.

 The platform builds for app developers.

 Framework; supports several languages and cross-language combinations.

 It also includes an IDE for writing programs.

 It is a set of utilities or in other words, building blocks of the user application


system.

 The framework provides a Graphical User Interface in the form of GUI.

 Moreover, .NET is an individual platform but it uses Mono Compilation


System (MCS) where it is a mid-level interface.

.Net Framework Architecture

The architecture of the .Net framework is a kind of programming model for the Net
platform under which it offers a managed performance environment, development, and
deployment in the simplest way. And the integration process with a wide range of coding
languages.
Furthermore, the.Net framework architecture is based on some key components that
include;
 CLS- Common Language Specification
 CTS- Common Type Specification
 FCL- .Net Framework Class Library
 CLR- Common Language Runtime
 CLI- Common Language Infrastructure
Moreover, there are different range of languages that .Net framework supports.
CLS- Common Language Specification
Common Language Specification or CLS includes a set of procedures that specifies
a .NET language. Apps build using several coding languages like C# and VB.NET, are
shorten down to CLS or Common Language Specification.
CTS- Common Type Specification

The Common Type System or CTS includes all types of data that are supported by
multiple languages. For Example;.NET types.System.Int32, System.Decimal, etc.

FCL (Framework Class Library)

The .NET Framework Class Library provides a large set of pre-built functions and
classes that can be used to create a wide range of applications. The library includes a
variety of namespaces that provide access to classes that provide features such as file
I/O, networking, database access, and graphical user interface (GUI) design.

Common Language Runtime (CLR):


The CLR is the runtime environment that manages and executes .NET applications.It
provides services such as memory management, exception handling, and security.
It includes a Just-In-Time (JIT) compiler that converts Intermediate Language (IL) code
into native machine code at runtime.
CLI- Common Language Infrastructure
The CLI or Common Language Infrastructure provides a language-independent platform
for app development and its performance. It also includes the function for exception
handling, waste collection, security, etc.

Features of.Net framework


The .NET Framework is a versatile platform with a wide range of features that facilitate
the development and execution of various types of applications. Here are some key
features of the .NET Framework:

Common Language Runtime (CLR):


The CLR provides a runtime environment for executing .NET applications.
It offers features such as memory management, exception handling, and security.
Code written in different languages is compiled into a common Intermediate Language
(IL) that runs on the CLR.
Base Class Library (BCL):
The BCL is a comprehensive library of pre-built classes that provide a wide range of
functionality.
It includes classes for tasks such as file I/O, networking, data access, cryptography, and
more.
Developers can leverage these classes to build applications more efficiently.
Language Independence:
.NET supports multiple programming languages, including C#, Visual Basic.NET, F#,
and others.
Code written in different languages can interoperate within the same application,
allowing developers to choose the language that best suits their needs.
Common Type System (CTS):
The CTS defines a common set of data types that all .NET languages must support.
It ensures type compatibility and seamless interaction between components written in
different languages.
Windows Forms:
Windows Forms is a graphical user interface (GUI) framework for building desktop
applications on the Windows platform.
It allows developers to create rich and interactive user interfaces using a drag-and-drop
design approach.
Web Services Support:
.NET supports the development and consumption of web services through technologies
like Windows Communication Foundation (WCF) and ASP.NET Web Services.
Security Features:
.NET includes a robust security model with features such as code access security, role-
based security, and cryptographic services. Code access security helps control the
permissions granted to code based on its origin and other factors.
Cross-language Exception Handling:
Exceptions thrown in one language can be caught in another language, promoting
seamless error handling across different components.
Interoperability:
.NET supports interoperability with existing COM components and libraries through
COM Interop. It also supports interoperability with native code through Platform
Invocation Services (PInvoke).
Portable Libraries:
Portable Class Libraries allow developers to create libraries that can be used across
multiple .NET platforms, enhancing code reuse.
Cross-Platform Development:
With the introduction of .NET Core and later versions, developers can create cross-
platform applications that run on Windows, macOS, and Linux. It's important to note that
some features may be specific to certain versions of the .NET Framework or its
successors like .NET Core and .NET 5 and later. The platform continues to evolve, and
new features may be introduced in newer releases.

Q2. Elaborate Origin and benefits of C#.

Ans:- C# is an object-oriented programming language created by Microsoft for


developing applications on the .NET platform. It allows developers to use a wide range of
.NET libraries and tools to build powerful, high-quality applications. In short, C# is a
powerful and versatile programming language that has become an essential tool for
software development on the .NET platform.

7 benefits of using C#

1. Easy learning curve: It is a high-level language with an easy learning curve,


making it accessible to programmers of all skill levels.

2. Security: It provides built-in security features, which help protect the application
against attacks and vulnerabilities.

3. Rapid development: Developers can write code faster and reduce the number of
errors and bugs.

4. Productivity:It offers high-productivity development tools such as IntelliSense


and advanced debugging, making code writing and debugging easier.

5. Cross-platform support: It facilitates the creation of applications for Windows,


Linux, and macOS..

6. Strong community: It has a strong and active community of developers,


providing support, resources, and solutions to common problems.

7. Profitability: It is a profitable and high-demand programming language, this


means that developers who master it can have a successful and well-paid career.
Q3. Explain variables, Comments, Identifiers and Constants in C#.

Ans:- In C#, variables, comments, identifiers, and constants are fundamental elements
used in writing and understanding code. Let's explore each of these concepts:

Variables in C#
Variables are the basic unit of storage in any program. The value stored in variables can
be modified during program execution.
The primary value types provided for Variables in C# can be categorized as follows:-

Types Description

int It stores integers without decimals, such as 120 or -120.

double It stores floating-point numbers with decimal values, such as 9.99 or -9.99.

char It stores single characters, like 'A' or 'b'. A single-quote pair surround char values.

string It stores multiple characters, such as "Hello World". A double quote surrounds a
String value.

bool It stores values with two states: false or true.

Defining Variables in C#
The syntax for defining variable in C# is −
<data_type> <variable_list>;

Naming conventions for Variables in C#


The naming convention for declaring a variable in C# is listed below:
.A variable name should be unique.
.A variable name can only contain digits, letters, and the underscore "_".
.A variable name must start with a letter.
.A variable name is case-sensitive; n and N are considered as two different names.
.A variable name cannot contain any reserved keywords (e.g. enum, break, case, etc.).
We must prefix @ before keyword if we want to use reserve keywords as identifiers.

Constants

If we declare a variable using the keyword "const", it is known as a constant variable. We


can't modify these constant variables once after their declaration, so we must initialize it
at the time of declaration only.

The const keyword is often useful when we want any variable always to store the same
value so that others (or ourselves) won't mess up with our code. For example, we often
refer to PI(3.14159…) as a constant variable. Let's see this example below:

const int myNum = 25;


myNum = 22; // error

Note: We cannot declare a constant variable without assigning a value. If we do so, an


error will occur: "A const field requires a value to be provided".

Identifiers

An identifier is an unique name given to an entity, which distinctly identifies an entity


in a program at the time of its execution. Identifiers are used for the naming of variable,
function, class, structure or a constant, etc. Once an identifier is declared, we can use the
identifier anywhere in the program to refer to the associated value.

The general rules for constructing unique identifiers are:

1. Names can contain letters(a-z,A-Z), digits(0-9) and underscores(_)


2. Names must begin with a letter or an underscore (_)
3. Names are case sensitive (testScore, testscore, Testscore are all different names)
4. Names cannot contain whitespaces or special characters like !, #, %, etc.
5. Reserved words (like C++ keywords, such as int) cannot be used as names

Identifer naming conventions

1. Variables: begin with a lowercase letter and capitalize each successive word.
2. Constants: capitalize every letter and use underscores to separate the English
words
3. Programmer written functions: will be capitalized in the same way as variable
names but they will begin with a capital letter.
Comments
The C# comments are statements that are not executed by the compiler. The comments in
C# programming can be used to provide explanation of the code, variable, method or
class. By the help of comments, you can hide the program code also.

There are two types of comments in C#.


 Single Line comment
 Multi Line comment
Single Line Comment

The single line comment starts with // (double slash). Let's see an example of single line
comment in C#.

Multi Line Comment

The C# multi line comment is used to comment multiple lines of code. It is surrounded
by slash and asterisk (/* ..... */). Let's see an example of multi line comment in C#.
Q4. Explain the data types in C#.
Ans:- In c# programming language, Data Types are useful to define a type of data the
variable can hold, such as integer, float, string, etc., in our application.

Syntax of Defining C# Data Types


Following is the syntax of defining data types in the c# programming language.

[Data Type] [Variable Name];


[Data Type] [Variable Name] = [Value];

There are 3 types of data types available in the C# language.

1. Value Data Types ( int, bool, char, double, float, etc.)


2. Reference Data Types ( string, class, object, interface, delegate, etc.)
3. Pointer Data Types (Pointers.)

Value Data Type in C#?

The data type which stores the value directly in the memory is called the Value Data
Type in C#. The examples are int, char, boolean, and float which store numbers,
alphabets, true/false, and floating-point numbers respectively.

The value data types in C# again classified into two types are as follows.
1. Predefined Data Types – Example includes Integer, Boolean, Boolean, Long,
Double, Float, etc.
2. User-defined Data Types – Example includes Structure, Enumerations, etc.

Reference Data Types

In c#, the Reference Data Types will contain a memory address of variable value
because the reference types won’t store the variable value directly in memory.
Again, the Reference Data Types are categorized into 2 types. They are as follows.

1. Predefined Types – Examples include Objects, Strings, and dynamics.

2. User-defined Types – Examples include Classes and Interfaces.

Pointer Type in C#?

The pointer in C# language is a variable, it is also known as a locator or indicator that


points to an address of the value which means pointer-type variables stores the memory
address of another type. To get the pointer details we have two symbols ampersand (&)
and asterisk (*).

1. ampersand (&): It is known as Address Operator. It is used to determine the


address of a variable.
2. asterisk (*): It is also known as Indirection Operator. It is used to access the value
of an address.
Q5. Define Operators in C#.
Ans:- In C#, operators are symbols that perform operations on operands. Operands are
the values or variables on which the operation is performed. C# supports a variety of
operators, which can be categorized into different types based on their functionality. Here
are some common types of operators in C#:

1. Arithmetic Operators:

+ (Addition): Adds two operands.


- (Subtraction): Subtracts the right operand from the left operand.
* (Multiplication): Multiplies two operands.
/ (Division): Divides the left operand by the right operand.
% (Modulus): Returns the remainder of the division of the left operand by the right
operand.

2. Relational Operators:

== (Equal to): Checks if two operands are equal.


!= (Not equal to): Checks if two operands are not equal.
> (Greater than): Checks if the left operand is greater than the right operand.
< (Less than): Checks if the left operand is less than the right operand.
>= (Greater than or equal to): Checks if the left operand is greater than or equal to
the right operand.
<= (Less than or equal to): Checks if the left operand is less than or equal to the
right operand.

3. Logical Operators:

&& (Logical AND): Returns true if both operands are true.


|| (Logical OR): Returns true if at least one operand is true.
! (Logical NOT): Returns true if the operand is false, and vice versa.

4. Assignment Operators:

= (Assignment): Assigns the value of the right operand to the left operand.
+= (Addition assignment): Adds the right operand to the left operand and assigns
the result to the left operand.

-= (Subtraction assignment): Subtracts the right operand from the left operand and
assigns the result to the left operand.

*= (Multiplication assignment): Multiplies the left operand by the right operand and
assigns the result to the left operand.

/= (Division assignment): Divides the left operand by the right operand and assigns
the result to the left operand.

%= (Modulus assignment): Computes the modulus of the left operand with the right
operand and assigns the result to the left operand.

5. Increment and Decrement Operators:

++ (Increment): Increases the value of the operand by 1.


-- (Decrement): Decreases the value of the operand by 1.
6. Conditional (Ternary) Operator:

? : (Conditional): Evaluates a condition and returns one of two values based on the
result of the condition.

7. Bitwise Operators:

& (Bitwise AND)


| (Bitwise OR)
^ (Bitwise XOR)
~ (Bitwise NOT)
<< (Left shift)
Q6. Explain Math, String, and Boolean in C#.
Ans:- Mathematical functions can be performed using
the System.Math class. System.Math is a static class that is included within .NET as part
of the System namespace. .NET is a free, cross-platform, open source developer platform
created by Microsoft.

The C# Math class has many methods that allows you to perform mathematical tasks on
numbers.

Below are a selected list of System.Math methods:

Math Functions
.Abs()

Returns the absolute value of a given number.

.Acos()

Returns the inverse cosine of the argument.

.Asin()

Returns an angle in radians whose sine is a specified number.

.Atan()

Returns the inverse tangent of the argument.

.Atan2()

Returns the angle, in radians, between the positive x-axis and the vector to point
(x, y).

.Ceiling()

Returns the smallest integer which is greater than or equal to a given number.

.Cos()

Returns the cosine of a given angle.

.Cosh()

Returns the hyperbolic cosine of a given angle.


.Exp()

Returns the result of raising e to the power of a specific number.

.Floor()

Returns the largest whole integer which is less than or equal to the given number.

.Max()

Returns the greater of two specified numbers.

.Min()

Returns the smaller of two specified numbers.

.Pow()

Returns the result of a given number raised to the power of a second number.

.Round()

Returns a value rounded to the nearest integer.

.Sin()

Returns the sine of a given angle.

.Sinh()

Returns the hyperbolic sine of a given angle.

.Sqrt()

Returns the square root of the given number.

.Tan()

Returns the tangent of a given angle.

.Tanh()

Computes the hyperbolic tangent of a given angle in radians.

C# Strings

In C#, string is an object of System.String class that represent sequence of characters.


We can perform many operations on strings such as concatenation, comparision, getting
substring, search, trim, replacement etc.
C# String methods
Method Name Description

Clone() It is used to return a reference to this instance of String.

Compare(String, String) It is used to compares two specified String objects. It returns an


integer that indicates their relative position in the sort order.

CompareOrdinal(String, It is used to compare two specified String objects by evaluating


String) the numeric values of the corresponding Char objects in each
string..

CompareTo(String) It is used to compare this instance with a specified String object.


It indicates whether this instance precedes, follows, or appears in
the same position in the sort order as the specified string.

Concat(String, String) It is used to concatenate two specified instances of String.

Contains(String) It is used to return a value indicating whether a specified


substring occurs within this string.

Copy(String) It is used to create a new instance of String with the same value as
a specified String.

CopyTo(Int32, Char[], Int32, It is used to copy a specified number of characters from a


Int32) specified position in this instance to a specified position in an
array of Unicode characters.

EndsWith(String) It is used to check that the end of this string instance matches the
specified string.

Equals(String, String) It is used to determine that two specified String objects have the
same value.

Format(String, Object) It is used to replace one or more format items in a specified string
with the string representation of a specified object.

GetEnumerator() It is used to retrieve an object that can iterate through the


individual characters in this string.

GetHashCode() It returns the hash code for this string.

GetType() It is used to get the Type of the current instance.

GetTypeCode() It is used to return the TypeCode for class String.

IndexOf(String) It is used to report the zero-based index of the first occurrence of


the specified string in this instance.

Insert(Int32, String) It is used to return a new string in which a specified string is


inserted at a specified index position.

Intern(String) It is used to retrieve the system's reference to the specified String.

IsInterned(String) It is used to retrieve a reference to a specified String.

IsNormalized() It is used to indicate that this string is in Unicode normalization


form C.

IsNullOrEmpty(String) It is used to indicate that the specified string is null or an Empty


string.

IsNullOrWhiteSpace(String) It is used to indicate whether a specified string is null, empty, or


consists only of white-space characters.

Boolean Type and Logical Operations:

The bool type represents Boolean values, which can be either true or false. Logical
operations are often performed using Boolean values:

Logical AND (&&): Returns true if both operands are true.


bool result = true && false; // Result is false
Logical OR (||): Returns true if at least one operand is true.
bool result = true || false; // Result is true
Logical NOT (!): Returns true if the operand is false, and vice versa.
bool result = !true; // Result is false
Relational Operators (==, !=, <, >, <=, >=): Compare two values and return a Boolean
result.
int a = 5, b = 10; bool isEqual = (a == b); // Result is false
Q7. Explain different types of Instructions in C#.
Ans:- In C#, instructions are typically written using statements. Statements are individual
units of code that perform specific actions. Here are some common types of instructions
in C#:
1. Declaration Instructions:
 Used to declare variables and specify their data types.

int age;
string name;
2. Assignment Instructions:
 Used to assign values to variables.

age = 25;
name = "John";
3. Arithmetic Instructions:
 Perform mathematical operations on variables or literals.

int sum = x + y;
int difference = x - y;

4. Control Flow Instructions:


 Control the flow of program execution based on conditions.
Examples include if, else, switch, while, do-while, for, etc.
if (condition) {
// Code to execute if the condition is true
}
else {
// Code to execute if the condition is false
}

5. Looping Instructions:
 Used to repeatedly execute a block of code.
Examples include while, do-while, for, and foreach.
\
for (int i = 0; i < 5; i++)
{
// Code to repeat five times
}
6. Jump Instructions:
 Control the flow of program execution by jumping to a specific location in the
code.
Examples include break, continue, return, and goto (though goto is rarely used and
often discouraged).
if (condition) {
break; // Exit the loop or switch statement
}
7. Method Call Instructions:
 Involve calling functions or methods to perform specific tasks.

Console.WriteLine("Hello, World!");

8. Exception Handling Instructions:


Used to handle exceptions that might occur during program execution.

Examples include try, catch, finally, and throw.


try
{
// Code that might throw an exception
}
catch (Exception ex)
{
// Code to handle the exception
}
finally
{
// Code that executes regardless of whether an exception occurred or not
}

9. Object Instantiation Instructions:


 Create instances of classes or objects.

MyClass myObject = new MyClass();

10.Array Instructions:
 Involve operations on arrays, such as element access and manipulation.

int[] numbers = { 1, 2, 3, 4, 5 };
int value = numbers[2]; // Access the third element
Q8. Explain Arrays in C#.
Ans:- Arrays are used to store multiple values in a single variable, instead of declaring
separate variables for each value.
To declare an array, define the variable type with square brackets: string[] cars;
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
int[] myNum = {10, 20, 30, 40};

 Array indexes start with 0: [0] is the first element. [1] is the second element, etc.

 C# array is an object of base type System.Array.

 A C# array variable can also be declared like other variables with [] after the data
type.

 A jagged array elements are reference types and are initialized to null.

Advantages of C# Array

 Optimization (less code)

 Random Access

 Easy to traverse data

 Easy to manipulate data

 Easy to sort data etc.

C# Array Types

There are 3 types of arrays in C# programming:

1. Single Dimensional Array

2. Multidimensional Array

3. Jagged Array

Single Dimensional Array


An array is used to store a collection of data, but it is often more useful to think of an
array as a collection of variables of the same type stored at contiguous memory locations.
OR single dimensional array, you need to use square brackets [] after the type.

int[] runs = new int[5]; //creating array

int[] runs = new int[5] {125, 173, 190, 264, 188};

EXAMPLE:-

using System;

namespace Program {

class Demo {

static void Main(string[] args) {

int[] runs = new int[5] {125, 173, 190, 264, 188};

int i,j;

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

Console.WriteLine("Innings score of Cricketer[{0}] = {1}", j, runs[j]);

Console.ReadKey();

Output:-

Innings score of Cricketer[0] = 125


Innings score of Cricketer[1] = 173
Innings score of Cricketer[2] = 190
Innings score of Cricketer[3] = 264
Innings score of Cricketer[4] = 188

Multidimensional Array
The multidimensional array is also known as rectangular arrays in C#. It can be two
dimensional or three dimensional. The data is stored in tabular form (row * column)
which is also known as matrix.

To create multidimensional array, we need to use comma inside the square brackets. For
example:

int[,] arr=new int[3,3]; //declaration of 2D array


int[,] numbers = { {1, 4, 2}, {3, 6, 8} }; //declaration of 2D array

int[,,] arr=new int[3,3,3]; //declaration of 3D array

EXAMPLE

using System;
public class MultiArrayExample
{
public static void Main(string[] args) {
int[,] arr=new int[3,3];//declaration of 2D array
arr[0,1]=10;//initialization
arr[1,2]=20;
arr[2,0]=30;

//traversal
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
Console.Write(arr[i,j]+" ");
}
Console.WriteLine();//new line at each row
}
}
}
Output:

0 10 0
0 0 20
30 0 0

Jagged Array

 Jagged array is a array of arrays such that member arrays can be of different sizes.
 The length of each array index can differ.
 The elements of Jagged Array are reference types and initialized to null by default.
 Jagged Array can also be mixed with multidimensional arrays.
 The number of rows will be fixed at the declaration time, but you can vary the
number of columns.
 The element size of jagged array can be different.
Syntax:

data_type[][] name_of_array = new data_type[rows][]

Example:-
int[][] jagged_arr = new int[4][]

You might also like