Sie sind auf Seite 1von 29

Q.1-Eight Difference B/W C# and C++.

1-C# compiles straight from source code to executable code, with no object file.
2-C# does not support # include statement.
3-All the data types in C# are inherited from Object super class therefore they are objects.
4-In C#,data types are belongs to either Value type or reference type.
5-In C# struct are value type.
6-C# does not support default arguments.
7-Casting in C# much safer than in C++.
8-C# does not provide any default for constructor.
9-C# does not support multiple code inheritance.

Q.2- .NET Framework


The .NET framework is one of the tool provided by the .NET infrastructure and tools component
of the .NET platform.

The .NET platform provides a new environment for creating and running robust, scalable and
distributed application over the web.

The .NET framework provides and environment for building, deploying and running web services
and other application. It consists of three distinct technologies as shown in figure.

*Common Language Runtime


*Framework Base Class
*User and program interface (ASP .NET and Win forms)
The CLR is the core of .NET framework and is responsible for loading and running C# programs.
Base class provide basic data types, collection classes and other general classes for use by C# and other
.NET languages. The top layer contains a set of classes for developing web services and to deal with the
user interface.

Q.3- Data Types in C#

*Integers *Enumerations *Objects *Classes


*Real Numbers *Structures *Strings *Arrays
*Boolean *Delegates
*Characters *Interfaces

Every variable in C# is associated with data types. Data types specify the size and type of value
can be stored. C# is a language rich in its data types. The variety available allows the programmer to
choose the appropriate data type which is need of the application.

The types in C# are primarily divided into two types:

*Value Types
*Reference Types

Value types and reference types are differ in two characteristics:

*Where they are stored in the memory.


*How they behave in the context of assignment statement

Value types are stored on stack, and when a value of the variable is assigned to other variable, The
value is actually copied. This means that two identical copies of the vale are available in memory.
Reference types are stored on heap and when assignment between two reference variable occurs,
only the reference is copied. The actual vale remains in the same memory location.
A third category of types called pointers is available for use only in unsafe code.
Q.4- Boxing and Unboxing
In Object oriented programming, Methods are invoked using objects. Since vale types such as int
and long are not objects, we cannot use them to call method. C# enable us to achieve this through a
technique known as Boxing. Boxing means the conversion of value type on the stack to object type on the
heap. Conversely, the conversion of object type in to value type is known as Unboxing.

*Boxing:
Any type,Value or reference type is assingned to an object without explicit conversion. When the
compiler finds the value type where it needs reference type it creats an object “BOX” in to which it places
the value of the value type.

Int m=100;
Object om= (object)m; // C-Style Casting

*Unboxing
Unboxing is the process of conversing object type to value type. We can only unbox a variable
that has previously been boxed.

Int m= 10;
Object om=m; //Box M
Int n=(int)om; //UNBOX om back to an int

Q.5- Abstract Class and Abstract Method

Abstract Class:

In a number of hierarchical applications, we would have a base class and the number of different
derived classes. The top most class is act as base class for others and it is not useful to its own. In such
situation, we might not want one to create it object. We can do this by using modifier Abstract.

The abstract is a modifier and when used to declare a class indicates that the class cannot be
instantiated. Only its derived class can be instantiated.

Abstract class Base


{
……..
}
Class derived : Base
{
…….
}
…………………….
Base b1;
Derived d1;

Some characteristics of abstract class are as follows.

* It cannot be instantiated directly.


* We cannot apply sealed modifier to it.
* It can have abstract members.
Abstract Method:

When an instance method declaration includes the modifier abstract, the method is said to be
abstract method. An abstract method is implicitly a virtual method and it does not provide any
implementation. Therefore, an abstract method does not have method body.

Public abstract void draw (int x, int y)

*It cannot have implementation.


*it cannot take either static or virtual modifier.
*It can be declare only in abstract class.

Q.6- What is CLR ? List Six different types of services provided by


CLR.

CLR is the core or heart and soul of the .NET framework and it is responsible for loading and
executing the C# programs and other .NET langages.It also supports cross language interoperability. CLR
provided the number of services.

*Loading and execution of programs


*memory isolation for application
*Memory management
*Managing exceptions and errors
*Enforcement of security
*Providing metadata
*Verification of type safety
Q.7- Explain Method Overloading.
Creating more than one method with the same name, but using different parameter list and
different definition is known as method overloading. Method overloading used when the methods required
to do the similar task but using different input parameter.

Overloaded methods must differ in numbers and type of parameter it takes. This enables the
compiler to decide which one of the definition to execute depending the type and number of the argument
in the method call.

Using system;
Class overloading
{
Public static void Main ()
{
Console.WriteLine(Volume (10));
Console.WriteLine(Volume (2.5 F , 8));
Console.WriteLine(Volume(100L , 75 , 15));
}
static int volume(intx)
{
Return (x * x * x);
}
Static double volume(float r , int h)
{
Return (3.14519 * r * r * h);
}
Static long volume(long l , b , h);
{
Return (l * b * h);
}
}

Q.8- Difference B/W Structure and Class.

Class Structures

* Data types are reference type therefore they are * Data types are value type therefore
Stored on heap. They are stored on stack
* It supports inheritance. * It does not support inheritance.
* It supports declaration of parameter less constructor. * It does not support declaration of
Parameter less constructors.
* It supports destructor. * It does not support destructors.
* Permits the initialization of instance fields. * It permits the initialization of
Instance field.
* Assignment copies the reference. * Assignment copies the values.
Q.9- Multiple Main Methods.

One of the class must have Main method as member. This is normally expected. However C#
includes a feature that enable us to define more than one class with the main method. Since the main is the
entry point for program execution, now there are two entry points. To resolve this problem by specifying
which main is use by compiler at the time of the compilation.

CSC file name.cs / main : class name


The file name is the name of the file in which code is stored and the class name is name of the
class containing the Main which would like to be the entry point.

Using system;
Class Class A
{
Public static void Main ()
{
Console.WriteLine (“Class A”);
}
}
Class Class B
{
Public static void Main ()
{
Consol.WriteLine (“Class B”);
}
}

Q.10- Command Line Arguments.

There may be occasions when we may like our program to behave in a particular way depending
on the input provided at the time of execution. This is achieved in C# by using what are known as
command line arguments. Command line arguments are parameter supplied to Main method at the time of
invoking it for execution.

Using System;
Class shadab
{
Public Static void Main (string [ ] args)
{
Console.Write (“welcome to ”);
Console.Write (“”+args [0]);
Console.WriteLine (“”+args [1]);
}
}

In this program Main is declared with a parameter args. The parameter args is declared as an array
of strings. Any arguments provided in the command line are passed to the array args as its elements. We
can access the array elements by using a subscript like args [0],args[1] and so on.

Shadab C Sharp
The command line contains two arguments which are assigned to the array args as follows:

C -------- args [0]


Sharp -------- args[1]

The output of the provided command line argument is as follows:

Welcome to C Sharp
Notice the use of new output method Console.WriteLine ()

Its only difference from the WriteLine () method is that Write () des not cause a line break and
therefore the next output statement is printed on the same line. Thus all the three output statements combine
to produse one line of output.

Q.11- Copy Constructor.

A copy constructor creates an object by copying a variable from another object. For example, we
may wish to pass an object type item to the item constructor so that the value of new item object is same as
the old one.

Public item (item item)


{
Code = item. Code;
Price = item. Price;
}

The copy constructor is invoked by the instantiating an object of type item and passing it the
object which is to be copied.

Item item2 = new item (item1);

Now item 2 is the copy of item 1.

Q.12- Switch Statement


The switch statement is test the value of the given variable (or expression) against the list of case
values and when match is found , a block of statement associated to that block is executed.

Switch (expression)
{
Case value-1:
Block-1
Break;
Case value-2:
Block-2
Break;
………………………
………………………
Default:
Default-block
Break;
}
Statement-x
The expression must be an integer type or char or string type.Value-1, Value-2,………. Are
constant or constant expressions and are known as case labels. Each of these values should be unique
within a switch statement. Block -1, block2,……..are statements and may contain zero to more statements.
There is no need to put braches around these blocks but it is important to note that case labels end with
colon (:).

Q.13- Difference between Java and C#

1- C# has more primitive data types.


2- Unlike java, All C# data types are objects.
3- Arrays are declared differently in C#.
4- Java uses static final to declare class constant while C# uses const.
5- C# supports the struct type and java does not.
6- Java des not have equivalent for C# indexers.
7- Java does not support directly to the enumerations.
8- C# provides static constructor for initialization.
9- Java does not provide for operator overloading.
10- C# provides better versioning support than java.

Q.14- Exeption handling in C#.

An exception is a condition that is cause by runtime-error in program. The basic concept of


exception handling are throwing an exception and catching it. C# uses the keyword try to preface a block
of code that is likely to cause the error condition and throws an exception. A catch block is define by the
keyword catch catches the exception thrown by the try block and handles it appropriately. The catch block
is added immediately after try block.

……………………
…………………… ttry ddd sssdsadsadsadsa
try
{
Statement;
}
Catch (Exception e)
{
Statement;
}
……………………
……………………

The try block can have one or more statements that could generate an exception. If any statement
generates an exception. The remaining statements in the try block are skipped and the execution is jumps to
catch block that is placed next to the try block. Every try statement should be followed by at least one catch
statement otherwise compilation error will occur.
Using System;
Class Error
{
Public static void Main ()
{
Int a = 10;
Int b = 5;
Int c = 5;
Int X, Y;

Try
{
X= a/(b-c);
}
Catch (exception e)
{
Console.WriteLine (“Division by zero”);
}
Y=a/ (b+c);
Console.WriteLine (“Y =” + Y);
}
}

Q.15- Operation Polymorphism.

The polymorphism one name and many forms. Essentially polymorphism is a capability of one
object to behave in many ways. Polymorphism can be achieved in two different ways as shown below

The operation polymorphism is implemented using overloaded methods and operators. The
methods are selected for invoking by matching arguments in terms of numbers, type and order. This
information is known to the compiler at the time of compilation therefore it selects and binds the
appropriate method to the object for a particular call at the time of compilation. This process is known as
early binding, static binding or static linking.

Using System;
Class DOG
{
}
Class CAT
{
}
Class operation
{
Static void Call (DOG d)
{
Console.WriteLine (“DOG is called”);
}
Static viod Call (CAT c)
{
Console.WriteLine (“CAT is called”);
}

Public static void Main ()


{
DOG dog = new dog ();
CAT cat = new cat ();
Call dog ();
Call cat ();
}
}
Q.16- Inclusion Polymorphism.

The inclusion polymorphism can be achieved through use of virtual functions. Assume that the
class a implements a virtual method M and class B and Class C are derived from class A override the
virtual method M. When class B is cast to class A, a call to the method M from class A is dispatched to
class B. Similarly when class C is cast to class A, a call to the virtual method M from class A is dispatched
to the class C. The decision of exactly which method to call delayed until the runtime and, therefore it is
also known as runtime polymorphism. Since the method is linked with a particular class is much later after
compilation, this process is known as late binding. It is also known as dynamic binding because the
selection of appropriate method is done dynamically at runtime.

Using System;
Class Maruthi
{
Public virtual void Display ()
{
Console.WriteLine (“Maruthi Car”);
}
}
Class Esteem : Maruthi
{
Public override void Display ()
{
Console.WriteLine (“Maruthi Esteem”);
}
}
Class Zen : Maruthi
{
Public override void Display ()
{
Console.WriteLine (“Maruthi Zen”);
}
}
Class Inclusion
{
Public static void Main ()
{
Maruthi m = new Maruthi ();
m = new Esteem ();
m.Display ();
m = new Zen ();
m.Display ();
}
}

When an object of Esteem is cast to m, then m behaves like the Esteem type object type. Similarly
when object of Zen is cast to m, then m is behave like Zen type object.
Q.17- Explain Inheritance and Classical Inheritance.

The mechanism of designing or constructing one class from another class is known as inheritance.
This may achieve in two different forms.

*Classical form
*Containment form

Classical Inheritance:
Inheritance represents a link between two classes. Let us consider two classes A and B. We can
create a hierarchy such that B is derived from A. class A is used as a basis for the derived classes is referred
as the base class, parent class, and super class. Class B is the derived class is refer as derived class, child
class and sub class. The derived class is a completely new class that incorporates all the data and method
members of its base class. It can also have it own data and method members that are unique to its itself.
The classical inheritance can be implemented in different combinations.

*Single Inheritance (Only one base class)


*Multilevel Inheritance (Derived from a derived class)
*Multiple Inheritance (Several base classes)
*Hierarchical Inheritance (One base class many sub classes)
Q.18- Explain Inheritance and Multilevel Inheritance.

The mechanism of designing or constructing new class from another class is known as inheritance.

Multilevel Inheritance:
The common requirement of object oriented programming is the use of derived class as a super
class. The multilevel inheritance allows us to build a chain of classes.

The Class A is act as a base class for class B and Class B is act as a base class for derived class C.
the chain of ABC is known as inheritance path.

Class A This process can be executed to any number of levels. The class C can inherit
{ The members of both class A and class B.
…………….
…………….
}
Class B : A
{
…………….
…………….
}
Class C : B
{
……………
……………
}

Class A
{
Protected int a;
Public A (int X)
{
X=a;
}
}
Class B : A
{
Protected int b;
Public B (int Y)
{
Y=b;
}
}
Class C : B
{
Protected int c;
Public C (int Z)
{
Z=c;
}
}

Q.19- Overriding a Method.

There may be occasion when we want an object to respond to same method but behave differently
when that method is called. That means, we should override a method defined in super class. This is
possible by defining a method in subclass with the same name, same arguments and same return type.
Then, when the method is called, the method defined in subclass will be invoked and executed instead of
the one defined in super class, provided that:

• We specify method in super class as VIRTUAL.


• Implement the method in subclass using keyword override.

Using System;
Class super
{
Protected int x;
Public super void Display ()
{
this.x = x;
}
Public virtual void Display ()
{
Console.WriteLine (“Super X=” +x);
}
}
Class sub : super
{
Int y;
Public sub (int x, int y) : base (x)
{
this.y = y ;
}
Public override void Display ()
{
Console.WriteLine (“Super x =” +x);
Console.WriteLine (“Sub y =” +y) ;
}
}
Class overrideTest
{
Public static void Main ()
{
Sub s1 = new Sub (100,200) ;
S1.Display () ;
}
}
Q.20- Explain the Properties.

C# provides mechanism known as properties that has the same capabilities as accessor methods,
but it is much more elegant and simple to use. Using property, a programmer can access the data members
as though they are public fields. The property is also referred as smart fields as they add the smartness to
data field.

Using system;
Class Number
{
Private int number;
Public int Anumber
{
Get
{
Return number;
}
Set
{
Number = value;
}
}
}
Class Property Test
{
Public static void Main ()
{
Number n = new number ();
n.Anumber = 100;
int m = n.Anumber;
Console.WriteLine (“Number m =” +m);
}
}

We have declared the property called Anumber of type int and get accessor method and set
accessor method. The getter method uses the key word return to return the field’s value to the caller. The
setter method uses the keyword value to receive the value being passed in from the user. The type of value
is determined by the type of property.

As the name implies, getter method is used to get the value and the setter method is used to set the
value.
n.Anumber

Invokes the setter method and places the integer value 100 in a variable named value which in turn
is assigned to the field number
Q.21- Explain Indexers.

Indexers are the location indicators and are used to access the class objects, just like accessing
elements in array. They are useful where a class is a container for the other objects.
An indexer look like a property and is written the same way property is written, but with two
differences.
• Indexer takes index argument and look like array.
• Indexer is declared using the name this.
The indexer implements through get and set accessors for the [ ] operator. For Example:

Public double this [int index]


{
Get { //Return desired data }
Set { //Set desired data }
}

The parameter declared in square brackets is used as index. The return type is determine
what will be written in this case double.

Using System;
Using Syatem.Collection
Class list
{
ArrayList array = new ArrayList ();
Public object this [int index]
{
Get
{
If (index<0 index >= array. count)
{
Return null;
}
Else
{
Return (array [index]);
}
Set
{
Array [index] = value;
}
}
}
Class IndexerTest
{
Public static void Main ()
{
List list = new List ();
list [0] = “123”;
list [1] = “abc”;
list [2] = “xyz”;
for (i = 0, i >=list.count; i++)
Console.WriteLine (list [i]);
}
}

Q.22- Read-Only Members

There are situations where we would like to decide the value of the constant member at run-time.
We may also like to have the different constant values for different objects of the class. To overcome these
shortcomings, C# provides the another modifier known as readonly to be used with the data members. This
modifier is design to set the value of the member using a constructor method, but cannot be modified later.
The readonly may be declared as either static fields or instance fields. When they are declared as instance
fields, they can take different values with different objects.

Class Number
{
Public readonly int m;
Public static readonly int n
Public Numbers (int x)
{
m = x;
}
Static Numbers ()
{
n = 100;
}
}

The value of m is provided at the time of creation of an object using the constructor with
parameter x. This value remains constant for this object. Remember, the variable n is assigned a value of
100 even before the creation of any objects on Numbers.

Q.23- Difference B/W Class and Interface.

• All the members of interface are implicitly public and abstract.


• Since the methods in interface are abstract, they do not have implementation code.
• It members cannot be declared as static.
• An interface cannot contain constant fields, constructors and destructors.
• An interface can inherit multiple interfaces.
Q.24- Explain the functions of the following.

* Add () Adds an object to a list.


* Clear () Removes all the elements from the list.
* Contains () Determines if an element is in the list.
* Copy To () Copies the list to another list.
* Insert () Insert an element in to the list.
* Remove () Removes the first occurrence of an element.
* Remove At () Removes the element at the specified place.
* RemoveRange () Removes a range of elements.
* Sort () Sorts the elements.
* Capacity () Gets or sets the number of elements in the list.
* Count () Gets the number of elements currently in the list.

Methods of system arrays:

* Clear () Sets a range of elements to empty values.


* CopyTo () Copies elements from the source array into the destination.
* GetLength () Gives the number of elements in a given dimension of the array.
* GetValue () Gets the value for a given index in the array.
* Length () Gives the length of an array.
* SetValue () Sets the value for a given index in the array.
* Reverse () Reverse the contents of a one-dimensional array.
* Sort () Sorts the elements in a one-dimensional array.

String Class Methods:

* IndexOf () Returns the position of the first occurrence of a substring.


* Substring () Extracts the substring.
* CompareTo () Compare the current instance with another instance.
* Concat () Concatenates two or more strings.
* LastIndexOf () Returns the position of last occurrence of a substring..
* Trim () Removes white space from the string.
Q.25- Explain Enumerations with Example.
An enumeration is a user-defined integer type which provides a way of attaching names
to numbers, thereby increasing the comprehensibility of the code. The enum keyword automatically
enumerates the list of word by assigning them values 0, 1, 2 and so on. The syntax of enum statement is as
follows:
enum shape
{ Circle, Square, Triangle }
This can be written in one line:
enum shape {circle, square, triangle}

Here, circle has the value 0, square has the value 1 and triangle has the value 2. Other examples of
enum are as follows:

enum color {red, blue, green}


enum days {Mon, Teu,Wed, Thus, Fri, Sat, Sun}

Using System;
Class Area
{
Public enum Shape
{
Circle,
Square
}
Public void AreaShape (int x, Shape shape)
{
Double area;
Switch (shape)
{
Case Shape.Circle:
area = math.p1*x*x;
Console.WriteLine (“Circle Area =” +area);
break;
case Shape.Square:
area = x*x;
Console.WriteLine (“Square Area =” +area);
break;
default:
Console.WriteLine (“Invalid Input”);
}
}
}

Class enumTest
{
Public static void Main ()
{
Area area = new Area ();
area.AreaShape (15, Area.Shape.Circle);
area.AreaShape (15, Area.Shape.Square);
area.AreaShape (15, (Area.Shape) 1);
area.AreaShape (15, (Area.Shape) 10);
}
}
Q.26- Explain the Interface and implementation of multiple Interface.

C# provides an alternate approach known as interface to support the concept of multiple


inheritance. An interface can have one or more methods, properties, indexer and events but none of them
are implemented in the interface itself. This is the responsibility of a class that implements the interface to
define the code for implementation of these members.

Below mentioned application shows the implementation of multiple interfaces. The class
computation implements two interfaces Addition and Multiplication. It declares two data members and
define the code for the method Add () and Mul ().

Using System;
interface Addition
{
Int Add ();
}
interface Multiplication
{
Int Mul ();
}
Class Computation : Addition, Multiplication
{
Int x, y;
Public Computation (int x , int y)
{
this.x = x;
this.y = y;
}
Public int Add ()
{
Return (x+y);
}
Public int Mul ()
{
Return (x*y);
}
}
Class interfaceTest
{
Public static void Main ()
{
Computation com = new Computaiton ();
Addition add = (Addition) com;
Console.WriteLine (“Sum =” +add.Add ());

Multiplication mul = (Multiplication) com;


Console.WriteLine (“Product =” +mul.Mul ());
}
}
Q.27- Explain Explicit Interface.

C# provides an alternate approach known as interface to support the concept of multiple


inheritance. An interface can have one or more methods, properties, indexer and events but none of them
are implemented in the interface itself. This is the responsibility of a class that implements the interface to
define the code for implementation of these members.

C# supports a technique known as explicit interface implementation, which allows a method to


specify the name of the interface it is implementing.

Using System;
Interface I1
{
I1 Display ();
}
Interface I2
{
I2 Display ();
}
Class C1 : I1,I2
{
Void I1.Display ()
{
Console.WriteLine (“I1 Display”) ;
}
Void I2.Display ()
{
Consol.WriteLine (“I2.Display”);
}
}
Class interfaceTest4
{
Public static void Main ()
{
C1 c = new C1 ();
I1 i1 = (I1) c;
i1.Display ();

I2 i2 = (I2) c;
i2.Display ();
}
}
Q.28- Explain Structures.

A structure in C# provides a unique way of packing together data of different types. It is a


convenient tool for handling a group of logically related data items. It creates a template which is use to
define its data properties. Once the structure has been defined, we can create variables of that type.
Structures are defined using struct key word.

Using System;
struct Item
{
Public string name;
Public int code;
Public double price;
}
Class StructTest
{
Public static void Main ()
{
Item fan;

Fan.name = “BAJAJ”;
Fan.code = “123”;
Fan.price = “1234.50”;

Console.WriteLine (“Fan Name:” +fan.name);


Console.WriteLine (“Fan Code:” + fan.code);
Console.WriteLine (“Fan Price:” + fan.price);
}
}
Q.29- Explain fallthrough in switch statement.

In the absence of break statement in a case block, if the controller is moved to the next case block
without any problem, it is known as fallthrough. Fallthrough is permitted in C, C++ and Java. But C# does
not permit automatic fallthrough. If the case block is containing executable code.

Switch (M)
{
Case 1:
X = M;
Case 2:
X = Y-M;
Default:
X = Y+M;
}

The above code will generate error. If we want two consecutive case blocks to be executed
continuously, we have to force the process by the use of goto statement.

Switch (M)
{
Case1:
X =M;
goto case2;
Case 2:
X = Y-M;
goto default;
default:
X = Y+M;
}
Q.30- Explain Pass by Value & Pass by Reference.
Pass by Value:
By default, method parameters are passed by value. That is, a parameter declared with no modifier
is passed by value and is called a value parameter. When method is invoked the values of the actual
parameter is assigned to the corresponding formal parameters. The value of the value parameter will be
changed within the method. The value of the actual parameter that is passed by value to a method is not
changed by any changes made to the corresponding formal parameter within the method body.

Using System;
Class passbyvalue
{
Static void change (int m)
{
m = m+10;
}
Public static void Main ()
{
Int x = 100;
Change (x);
Console.WriteLine (“X =” + x);
}
}

Pass by Reference:
By default, method parameters are passed by value. We can however, force the value parameter to
be passed by reference. To do this we use ref key word. A parameter declared with the ref modifier is a
reference parameter for Example:
void Modify (ref int x)
Here x is declared as the reference parameter. Unlike value parameter, a reference parameter does
not crate a new storage location. It represents the same storage location as the actual parameter used in the
method invocation.

Using System;
Class Passbyref
{
Static void Swap (ref int x, ref int y)
{
Int temp = x;
x = y;
y = temp;
}
Public static void Main ()
{
Int m = 100;
Int n = 200;
Console.WriteLine (“Before Swapping”);
Console.WriteLine (“m =” +m);
Console.WriteLine (“n =” +n);
Swap (ref m, ref n);
Console.WriteLine (“After Swapping”);
Console.WriteLine (“m =” +m);
Console.WriteLine (“n =” +n);
}
}
Q.31- Explain the Output Parameters.
The Output Parameters:
The output parameters are used to pass result back to the calling method. This is achieved
by declaring the parameters with an out keyword. Similar to the reference parameter, it does not create a
new storage location. Instead, it becomes the alias to the parameter in the calling method. When a formal
parameter is declared with out, the correspondence actual parameter in the calling method must also be
declared as out.

Using System;
Class Output
{
Static void Square (int x, out int y)
{
y = x*x;
}
Public static void Main ()
{
Int m;
Square (10, out m);
Console.WriteLine (“m =” + m);
}
}

Q.32- Explain Variable Argument List OR Parameter Arrays.


Variable Argument List:
In C#, we can define a method that can handle variable number of argument using what are known
as parameter arrays. Parameter arrays are declared using the keyword params.

void function1 (params int [] x)


{
……….
}
Here, x has been declared as a parameter array. The parameter arrays must be one-dimensional
arrays.

Using System
Class Params
{
Static void Parray (params int [ ] arr)
{
Console.WriteLine (“Arrays elements are :”);
foreach (int i in arr)
Console.WriteLine (“” + i);
Console.WriteLine ();
}
Public static void Main ()
{
Int [] x = {11, 22, 33};
Parray (x);
Parray ();
Parray (100, 200);
}
}
Q.33- Explain Array List Class.

System. Collection namespace defines a class known as Array List that is used to store
dynamically sized array of objects. Array List class includes a number of methods that supports the
operations such as sorting, removing and enumerating the contents. It also supports a property count that
gives the number of object present in the array list and a property capacity to modify or read the capacity.

Using System;
Using System. Collection;
Class city
{
Public static void Main ()
{
ArrayList n = new ArrayList ();
n.Add (“Mumbai”);
n.Add (“Delhi”);
n.Add (“Calcutta”);
n.Add (“Anand”);
n.Add (“Madras”);
Console.WriteLine (“Capacity =” + n.capacity);
Console.WriteLine (“Number of Elements = ” + n.Count);
n.sort ();
for (i = 0, I <n.count; i++)
{
Console.WriteLine (n[i]);
}
Console.WriteLine ();
n.RemoveAt (4);
for (i = 0, I < n.count; i++);
{
Console.WriteLine (n[i]);
}
}
}
Q.34- Overloading Unary Operator.

Using System;
Class Space
{
Int x, y, z;
Public Space (int a, int b, int c)
{
x = a;
y = b;
z = c;
}
Public void Display ()
{
Console.Write (“” + x);
Console.Write (“” + y);
Console.Write (“” + z);
}
Public static void operator – (Space s)
{
s.x = -s.x;
s.y = -s.y;
s.z = -s.z;
}
}
Class Test
{
Public static void Main ()
{
Space s = new Space (10, -20, 30);
Console.WriteLine (“s :”);
s.Display ();
-s;
Console.WriteLine (“s :”);
s.Display ();
}
}
Q.34- Overloading Binary Op erator.

Using System;
Class Complex
{
Double x;
Double y;
Public complex ()
{
x = real;
y = imag;
}
Public static Complex operator + (Complex c1, Complex c2)
{
Complex c3
c3.x = c1.x + c2.x;
c3.y = c1.y + c2.y;
return (c3);
}
Public void Display ()
{
Console.Write (x);
Console.Write (“+j” + y);
Console.WriteLine ();
}
}
Class Test
{
Public static void Main ()
{
Complex a, b, c;
a = (2.5, 3.5);
b = (3.8, 1.6);
c = a+b;

Console.Write (“a =”)


a.Display ();
Console.Write (“b =”);
b.Display ();
Console.Write (“c =”);
c.Display ();
}
}

Das könnte Ihnen auch gefallen