Sei sulla pagina 1di 8

ASSIGNMENT-1

OF
CSE 406

SUBMITTED TO SUBMITTED BY
Mr .P.S VIJENDRA SATISH KUMAR PANDEY
LPU B.Tech (H) MBA {IT}
PART- A
1. What standard data types does C Sharp use? Contrast it with the types in C++?

Ans.
C# supports a very similar range of basic types to C++, including int, long, float, double, char,
string, arrays, structs and classes. However, don't assume too much. The names may be familiar,
but many of the details are different. For example, a long is 64 bits in C#, whereas in C++ the size
of a long depends on the platform (typically 32 bits on a 32-bit platform, 64 bits on a 64-bit
platform). Also classes and structs are almost the same in C++ - this is not true for C#. Finally,
chars and strings in .NET are 16-bit (Unicode/UTF-16), not 8-bit like C++.

2 Does C Sharp support variable argument on method? Explain with the help of an example.

ANS. Yes. The params keyword can be applied to a method parameter which is an array. Upon
method invocation, the array elements can be supplied as a comma (,) separated list.

For example,
if the method parameter were an array of object, the source code would be similar to the
following:

void paramsExample (object argument-1, object argument-n, params object[]


variableArguments)
{
foreach (object arg in variableArguments)
{
}
}

And, this method can be invoked with any number of arguments of any type. Here is a sample
invocation:
paramsExample (1, 2.3m, 4.5f, "arbitrary string", new UserDefinedType());
But, many developers would prefer to pass a variable number of arguments of a single, specific
type—e.g. string—rather than object.

2. What’s the difference between const and read-only?

ANS. This source code below is an an example of the difference between const and
readonly. Say you created a file A.cs and then compiled it to A.dll. Then you write your main
application called MyTest.cs. After compiling MyTest.cs with referencing A.dll, you run the
MyTest.exe.

using System;
public class A
{
public const int X = 123;
}
csc /t:library /out:A.dll A.cs

using System;
public class MyTest
{
public static void Main()
{
Console.WriteLine("X value = {0}", A.X);
}
}

csc /r:A.dll MyTest.cs

To run: mytest
The output :
X value = 123

Then you install the program into your client computer. It runs perfectly.
One week later, you realised that the value of X should have been 812 instead of 123.
What you will need to do is to

1] Compile A (after making the changes)


csc /t:library /out:A.dll A.cs

2] Compile your application again


csc /r:A.dll MyTest.cs

This can be a little troublesome. However, if you used the readonly instead of const,the
situation will be slightly different. You start with

using System;
public class A
{
public static readonly int X = 123;
}

csc /t:library /out:A.dll A.cs

using System;
public class MyTest
{
public static void Main()
{
Console.WriteLine("X value = {0}", A.X);
}
}

csc /r:A.dll MyTest.cs

To run: mytest
The output :
X value = 123
Now you realised, you have made a mistake. All you need to do is

1] Recompile A.cs (after making changes)


csc /t:library /out:A.dll A.cs

2] Copy the new dll to the client computer and it should run perfectly. There is no need to
recompile your application MyTest.cs here

Part – B

3. What is the difference about Switch statement in C Sharp from C++ and C?

ANS. The switch Statement

C# is a distinct language from C++. C++ is designed for general object oriented
programming in the days when the typical computer was a standalone machine running a
command line-based user interface. C++ is a general-purpose programming language with
high-level and low-level capabilities. It is a statically typed, free-form, multi-paradigm,
usually compiled language supporting procedural programming, data abstraction, object-
oriented programming, and generic programming.

C++ is regarded as a mid-level language. This indicates that C++ comprises a combination
of both high-level and low-level language features. C# is designed specifically to work with
the .Net and is geared to the modern environment of Windows and mouse-controlled user
interface, networks and the internet.

Another form of selection statement is the switch statement, which executes


a set of logic depending on the value of a given parameter. The types of the
values a switch statement operates on can be booleans, enums, integral
types, and strings.

. Switch Statements: SwitchSelection.cs


using System;

class SwitchSelect
{
public static void Main()
{
string myInput;
int myInt;

begin:

Console.Write("Please enter a number between 1 and 3: ");


myInput = Console.ReadLine();
myInt = Int32.Parse(myInput);

// switch with integer type


switch (myInt)
{
case 1:
Console.WriteLine("Your number is {0}.", myInt);
break;
case 2:
Console.WriteLine("Your number is {0}.", myInt);
break;
case 3:
Console.WriteLine("Your number is {0}.", myInt);
break;
default:
Console.WriteLine("Your number {0} is not between 1 and 3.", myInt);
break;
}

decide:

Console.Write("Type \"continue\" to go on or \"quit\" to stop: ");


myInput = Console.ReadLine();

// switch with string type


switch (myInput)
{
case "continue":
goto begin;
case "quit":
Console.WriteLine("Bye.");
break;
default:
Console.WriteLine("Your input {0} is incorrect.", myInput);
goto decide;
}
}
}
4. What is Method Overriding? How to override a function in C Sharp?

ANS. Method Overriding if changing the behavior of function in derived class, defined in the
base class.to override a method in the derived class declare the function virtual in the base
class.

Only if a method is declared virtual, derived classes can override this method if they are
explicitly declared to override the virtual base class method with the override keyword.

FOR EXAMPLE

using System;
namespace Polymorphism
{
class A
{
public virtual void Foo() { Console.WriteLine("A::Foo()"); }
}

class B : A
{
public override void Foo() { Console.WriteLine("B::Foo()"); }
}

class Test
{
static void Main(string[] args)
{
A a;
B b;

a = new A();
b = new B();
a.Foo(); // output --> "A::Foo()"
b.Foo(); // output --> "B::Foo()"

a = new B();
a.Foo(); // output --> "B::Foo()"
}
}
}
5. What is the difference between method parameters and method arguments? Give an example?

ANS. Formal Parameters

A method's parameters are the types that get passed to it when the method is called. The list of
parameters begins by specifying zero or more 'fixed parameters', and it may finish by specifying
a single parameter-array. This latter element - declared using the 'params' keyword - means that
it is possible to pass an arbitrary number of types to a single method. An example is given later
in the lesson.

Fixed parameter specifications can have either two or three parts (ignoring attributes). The first,
optional modifier can be either 'ref' or 'out'. The second part of the specification specifies the
parameter's type, and the third part its name. Examples of these different elements can be seen in
the illustrative code in the sections below.

[Modifier] parameter-type parameter-identifier

Parameter Passing

passing by value

The parameter modifiers 'ref' and 'out' relate to how the parameter is passed into the method.
Where neither of these modifiers is used, the parameter is passed in 'by value'. In this case, when
the method is called the value given is copied to the variable specified in the method declaration.
The following example illustrates this point; note that the change made to variable b in the body
of the 'change' method doesn't result in a change to the variable a used to invoke the method.

public static void Main()


{
int a = 0;
change(a); // following this method invocation, a equals 0
}
public static void change(int b)
{
b = 5;
}

In this example, it was a value type that was passed 'by value'. But reference types can also be
passed 'by value'. As we saw previously, the immediate value held by a reference type variable is
actually a memory address. So when this variable is passed 'by value', the memory address is
copied to the variable specified in the method head. But of course, because the two variables will
hold the same memory address, any changes made within the method body to the object located
at that memory address will be reflected outside the method (although this doesn't apply for
immutable reference types like strings.

passing by reference

In C# we can pass variables into methods 'by reference'. Where a variable is passed by reference,
the 'ref' modifier must be used both in the method head and the method invocation (illustrated by
the next code block).

Passing by reference is most obviously useful in cases where we want to treat a value type like a
reference type. For instance, the method call in the following code does change the value of the
variable a passed into the 'change' method.

public static void Main()


{
int a = 0;
change(ref a); // following this method invocation, a==5
}
public static void change (ref int b)
{
b = 5;
. }

Potrebbero piacerti anche