Assignment C#pathak
Assignment C#pathak
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.
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.
7 benefits of using C#
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.
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
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.
Defining Variables in C#
The syntax for defining variable in C# is −
<data_type> <variable_list>;
Constants
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:
Identifiers
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.
The single line comment starts with // (double slash). Let's see an example of single line
comment in C#.
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.
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.
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. Arithmetic Operators:
2. Relational Operators:
3. Logical Operators:
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.
? : (Conditional): Evaluates a condition and returns one of two values based on the
result of the condition.
7. Bitwise Operators:
The C# Math class has many methods that allows you to perform mathematical tasks on
numbers.
Math Functions
.Abs()
.Acos()
.Asin()
.Atan()
.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()
.Cosh()
.Floor()
Returns the largest whole integer which is less than or equal to the given number.
.Max()
.Min()
.Pow()
Returns the result of a given number raised to the power of a second number.
.Round()
.Sin()
.Sinh()
.Sqrt()
.Tan()
.Tanh()
C# Strings
Copy(String) It is used to create a new instance of String with the same value as
a specified String.
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.
The bool type represents Boolean values, which can be either true or false. Logical
operations are often performed using Boolean values:
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;
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!");
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.
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
Random Access
C# Array Types
2. Multidimensional Array
3. Jagged Array
EXAMPLE:-
using System;
namespace Program {
class Demo {
int i,j;
Console.ReadKey();
Output:-
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:
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:
Example:-
int[][] jagged_arr = new int[4][]