Sie sind auf Seite 1von 8

[Winter 2014] ASSIGNMENT

PROGRAM Master of Science in Information Technology (MSc IT) Revised Fall


2011
SEMESTER 4
SUBJECT CODE & NAME MIT4021 C# and .Net
CREDIT 4 BK ID B1187 MAX. MARKS 60

Q.No 1 What is Common Language Runtime (CLR)? List the services that CLR
provide. [3 +7] 10
Answer:
The Common Language Runtime (CLR)
The Common Language Runtime or CLR is the .NET equivalent of Java Virtual Machine (JVM).
The primary goal of the CLR is to locate, load, and manage .NET types. The term runtime can be
understood as a collection of external services that are required to execute a given compile unit
of code.

The CLR provides a number of services that include:


Loading and Execution of programs
Memory isolation for applications
Verification of Type safety
Compilation of IL into native executable code
Providing meta data
Automatic memory management (Garbage Collection)
Enforcement of Security
Interoperability with other systems
Managing exceptions and errors
Support for tasks such as debugging and profiling

2 Explain (a) Assignment Operators (b) Bitwise Operators. [5 +5] 10


Answer:
a) Assignment Operators
Assignment operators are used to assign the value of an expression to a variable. We have seen
the usual assignment operator, =.In addition, C# has a set of shorthand assignment operators
which are used in the form
Syntax: V op = exp;
Where V is a variable, exp is an expression and op is a C# binary operator.
The operator op = is known as the shorthand assignment operator.
The assignment statement:
V op=exp
is equivalent to
V = v op (exp);
b) Bitwise Operators
C# supports operators that may be used for manipulation of data at bit level. These operators
are used for testing the bits or shifting them to the right or left. Bitwise operators may not be
used for manipulating the floating point data. The table 4.7 gives the bitwise logical and shift
operators used in C#.
Table 4.7: Bitwise Operators and their Meaning
Operator Meaning
&

Bitwise logical AND

Bitwise logical OR

Bitwise logical XOR

Ones Complement

<<

Shift left

>>

Shift right

3. Explain different decision making statements used in C#. [10]

Answer:
C# language supports the following control or decision making statements:
1) if statement
2) switch statement
3) conditional operator statement
The if statement is a powerful decision making statement and is used to control the flow of
execution. It is a two way decision statement and is used in conjunction with an expression. It
takes the following forms:
1) Simple if statement
2) ifelse statement
3) Nested ifelse statement
4) elseif ladder
Simple if
The general form of a simple if statement is shown below. The statement block may be a single
statement or a group of statements. If the Boolean expression is true, the statement block will be
executed; otherwise the statement block will be skipped and the execution will jump to
statement-x. If the condition is true both the statement block and the statement x is executed
in sequence.
ifelse
The ifelse construct tests a condition and if that condition is true, it executes either the next
code statement or several code statements if they are enclosed in curly braces. If the condition
does not evaluate to true, then it either skips the code segment, or executes code in an else
statement.
Nested ifelse
When a series of decisions are involved, we may have to use more than one ifelse statement in
nested form as follows:
Note: Writing an if statement within the scope of another if statement is known as Nested If.
The ifelseif Ladder

There may be situations wherein we need multiple if statements nested within each other to
check and execute multiple conditions. In case, where none of the conditions within these if
statements is satisfied, then the default statement following the last else would be executed.

4 What is a structure in C#? Explain with an example. [3 +7]=10


Answer:
Defining a Structure
A structure in C# provides a unique way of packing together data of different types. It is a
convienient tool for handling a group of logically related data items.
A structure is a template that defines the data and the corresponding data types it holds in the
context of an application. Once the structure has been defined, we can create variables of struct
type using declarations that are similar to the built in type declarations.
Syntax:
[attributes] [modifiers] struct <struct-name> [: interfaces]
{
//Body of the structure
} [;]
Example:
struct student
{
public string name;
public int rollnumber;
public double totalmarks;
}
The keyword struct declares student as a new data type than can hold three variables of different
data types. These variables are known as members or fields or elements. The identifier student
can now be used to create variables of type Student.

5 What is hiding a method? Explain. [3 +7] 10


Answer:
In overriding a base class method, we declared the base class method as virtual and the subclass
method with the key override. This resulted in hiding the base class method from the subclass.
Now, let us assume that we wish to derive from a class provided by someone else and we also
want to redefine some methods contained in it. Here, we cannot declare the base class methods
as virtual. Then, how do we override a method without declaring it virtual. This is possible in C#
by using the modifier new to tell the compiler that the derived class method hides the base
class method. The program 10.4 shows how to hide base class methods in a derived class.

Program 10.4 :Hiding base class methods


using System;
class Base
{
public void Display()
{
Console.WriteLine("Base method");
}
}
class Derived : Base
{
public new void Display() //hides base method
{
Console.WriteLine("Derived Method");
}
}
class HideTest
{
public static void Main()

{
Derived d = new Derived();
d.Display();
Console.Read();
}
}

6 What is an event? Explain with example. [3 +7] 10


Answer:
An event is a delegate type class member that is used by the object or class to provide a
notification to other objects that an event has occurred. The client object can act on an event by
adding an event handler to the event.
Events are declared using the simple event declaration format as follows:
Modifier event type event-name;
The modifier may be new, a valid combination of the four access modifiers, and a valid
combination of static, virtual, override, abstract, and sealed. The type of an event declaration
must be a delegate type and the delegate must be accessible as the event itself. The event name is
any valid C# variable name. Event type is the keyword that signifies that the event name is the
name of the event being dealt with.
Example:
public event EventHandler click;
public event RateChange Rate;
The code of below program shows the implementation of event handlers.

Program: Implementing an Event Handler


using System;

//delegate declaration first


public delegate void Edelegate(string str);
class Eventclass
{
//declaration of event
public event Edelegate Status;
public void TriggerEvent()
{
if (Status != null)
Status("Event Triggered");
}
}
class Eventtest
{
public static void Main()
{
Eventclass ec = new Eventclass();
Eventtest et = new Eventtest();
ec.Status += new Edelegate(et.Eventcatch);
ec.TriggerEvent();
Console.Read();
}
public void Eventcatch(string str)
{
Console.WriteLine(str);
}
}
The program declared first a delegate which is used to declare the event status in the class
EventClass. This class has a method to trigger the event when it occurs. Note that we have to
check the event Status against null because no event might have occurred earlier. The class
Eventtest defines, in addition to the Main, a method EventCatch whose signature matches that
of the event. The Eventclass is instantiated, and the method is subscribed to the status event:

ec.Status+=new Edelegate(et.EventCatch); This method would be called when the event is


triggered.

Das könnte Ihnen auch gefallen