You are on page 1of 8


CSE 406

LPU B.Tech (H) MBA {IT}

"arbitrary string". specific type—e. PART. However. new UserDefinedType()).cs and then compiled it to A.3m. the array elements can be supplied as a comma (. double. For example. but many of the details are different. 2 Does C Sharp support variable argument on method? Explain with the help of an example.cs with referencing A. chars and strings in .) separated list. object argument-n. not 8-bit like C++. many developers would prefer to pass a variable number of arguments of a single. public class A { public const int X = 123. Then you write your main application called MyTest. string—rather than object. char.dll. What’s the difference between const and read-only? ANS.NET are 16-bit (Unicode/UTF-16). 4. For example. ANS. Say you created a file A. don't assume too much. long. whereas in C++ the size of a long depends on the platform (typically 32 bits on a 32-bit platform. The names may be familiar. Also classes and structs are almost the same in C++ . After compiling MyTest. the source code would be similar to the following: void paramsExample (object argument-1.5f.A 1. you run the MyTest. But.dll.exe. What standard data types does C Sharp use? Contrast it with the types in C++? Ans. 2. including int. This source code below is an an example of the difference between const and readonly. Upon method invocation. 64 bits on a 64-bit platform). this method can be invoked with any number of arguments of any type.this is not true for C#. using System. structs and classes. Yes. arrays. } . Finally. The params keyword can be applied to a method parameter which is an array. 2. float. a long is 64 bits in C#. Here is a sample invocation: paramsExample (1.g. C# supports a very similar range of basic types to C++.cs. params object[] variableArguments) { foreach (object arg in variableArguments) { } } And. string. if the method parameter were an array of object.

the situation will be slightly different. } } csc /r:A.dll MyTest. It runs perfectly. if you used the readonly instead of const. public class A { public static readonly int X = 123.dll A.WriteLine("X value = {0}".cs To run: mytest The output : X value = 123 . You start with using System.cs using System. you realised that the value of X should have been 812 instead of 123.dll A.WriteLine("X value = {0}". } csc /t:library /out:A.cs using System.dll MyTest. However. public class MyTest { public static void Main() { Console. public class MyTest { public static void Main() { Console.cs This can be a little troublesome. A.dll A.X).dll /t:library /out:A. A.X). } } csc /r:A.cs 2] Compile your application again csc /r:A. One week later.cs To run: mytest The output : X value = 123 Then you install the program into your client computer. What you will need to do is to 1] Compile A (after making the changes) csc /t:library /out:A.

cs 2] Copy the new dll to the client computer and it should run perfectly.dll A. 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.Net and is geared to the modern environment of Windows and mouse-controlled user interface. and generic programming. Switch Statements: SwitchSelection. and strings.cs (after making changes) csc /t:library /out:A. usually compiled language supporting procedural programming. you have made a mistake. object- oriented programming. All you need to do is 1] Recompile A. Another form of selection statement is the switch statement. integral types. C++ is regarded as a mid-level language. which executes a set of logic depending on the value of a given parameter. networks and the internet.cs using System. The switch Statement C# is a distinct language from C++. The types of the values a switch statement operates on can be booleans. class SwitchSelect { public static void Main() . free-form. enums.Now you realised. C++ is a general-purpose programming language with high-level and low-level capabilities. multi-paradigm. There is no need to recompile your application MyTest. This indicates that C++ comprises a combination of both high-level and low-level language features. . data abstraction.cs here Part – B 3. C# is designed specifically to work with the . It is a statically typed. What is the difference about Switch statement in C Sharp from C++ and C? ANS.

Write("Type \"continue\" to go on or \"quit\" to stop: ").Parse(myInput).". } } } .ReadLine(). default: Console. myInput = Console. myInt). myInt).". goto decide. break. begin: Console.WriteLine("Your number is {0}.WriteLine("Your number {0} is not between 1 and 3. myInt). case "quit": Console.Write("Please enter a number between 1 and 3: "). int myInt. myInput = Console."). break.ReadLine(). { string myInput. break. } decide: Console.WriteLine("Your input {0} is incorrect.". case 2: Console. // switch with string type switch (myInput) { case "continue": goto begin. // switch with integer type switch (myInt) { case 1: Console. default: Console.WriteLine("Your number is {0}. case 3: Console.". myInput). break.".WriteLine("Bye. break.WriteLine("Your number is {0}. myInt = Int32. myInt).

defined in the base class. b = new B(). override a method in the derived class declare the function virtual in the base class. // output --> "B::Foo()" a = new B(). a.Foo(). } } class Test { static void Main(string[] args) { A a. // output --> "B::Foo()" } } } .Foo(). a. FOR EXAMPLE using System. } } class B : A { public override void Foo() { Console. Only if a method is declared virtual.WriteLine("A::Foo()"). Method Overriding if changing the behavior of function in derived class. namespace Polymorphism { class A { public virtual void Foo() { Console.Foo(). B b. a = new A().WriteLine("B::Foo()"). // output --> "A::Foo()" b. What is Method Overriding? How to override a function in C Sharp? ANS. derived classes can override this method if they are explicitly declared to override the virtual base class method with the override keyword.

So when this variable is passed 'by value'. The following example illustrates this point. the immediate value held by a reference type variable is actually a memory address. and the third part its name.means that it is possible to pass an arbitrary number of types to a single method. Examples of these different elements can be seen in the illustrative code in the sections below. it was a value type that was passed 'by value'. An example is given later in the lesson. The first. a equals 0 } public static void change(int b) { b = 5. public static void Main() { int a = 0.declared using the 'params' keyword . when the method is called the value given is copied to the variable specified in the method declaration. optional modifier can be either 'ref' or 'out'. 5. In this case. } In this example. This latter element . [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. But reference types can also be passed 'by value'. and it may finish by specifying a single parameter-array. The list of parameters begins by specifying zero or more 'fixed parameters'. because the two variables will hold the same memory address. // following this method invocation. 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. any changes made within the method body to the object located . Where neither of these modifiers is used. The second part of the specification specifies the parameter's type. Fixed parameter specifications can have either two or three parts (ignoring attributes). the parameter is passed in 'by value'. As we saw previously. But of course. 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 memory address is copied to the variable specified in the method head. change(a).

Passing by reference is most obviously useful in cases where we want to treat a value type like a reference type. passing by reference In C# we can pass variables into methods 'by reference'. Where a variable is passed by reference. . the method call in the following code does change the value of the variable a passed into the 'change' method. a==5 } public static void change (ref int b) { b = 5. public static void Main() { int a = 0. // following this method invocation. } . change(ref a). the 'ref' modifier must be used both in the method head and the method invocation (illustrated by the next code block).at that memory address will be reflected outside the method (although this doesn't apply for immutable reference types like strings. For instance.