Chettinad College of Engineering and Technology C# and .NET Framework

CHETTINAD COLLEGE OF ENGINEERING AND TECHNOLOGY

DEPARTMENT OF INFORMATION TECHNOLOGY

C# AND .NET FRAMEWORK

UNIT – I

Part – A Two Marks

1. What is C#?

  1. C# (pronounced as 'c’ sharp') is a new computer-programming language developed by Microsoft Corporation, USA.
  2. C# is a fully object-orientedlanguage like Java and is the first Component- orientedlanguage. It has been designed to support the key features of .NET Framework, the new development platform of Microsoft for building component-based software solutions.

2. What are the characteristic for the C#?

1. Simple2. Consistent3. Modern4. Object – Oriented

5. Type – Safe6. Versionable 7. Compatible8. Interoperable

9. Flexible

3. List out application for the C#.

  • Console applications
  • Windows applications
  • Developing Windows controls
  • Developing ASP.NET projects
  • Creating Web controls
  • Providing Web services
  • Developing .NET component library

4. How does C# differ from C++?

  • C# does not support #include statement. (Note that using is not the sameas#include).
  • C# checks for uninitialized variables and gives error messages at compile time. In C++, an uninitialized variable goes undetected thus resulting in unpredictable output.
  • C# supports four iteration statements rather than three in C++. The fourth oneisthe foreach statement.
  • When overriding a virtual method, we must use the override keyword.
  • Abstract methods in C# are similar to virtual functions in C++, but C# abstractmethods cannot have implementations.

5. What are the C++ features dropped in C#?

The following C++ features are missing from C#:

1. Macros2. Multiple inheritances3. Templates

4. Pointers5. Global variables6. Typedef statement

7. Default arguments8. Constant member functions or parameters

9. Forward declaration of classes

6. What are features Enhancements to C++ in C#?

1. Automatic garbage collection2. Versioning support

3. Strict type-safety4. Delegates and events5. Boxing and unboxing 6. Web services

7. How does C# differs from Java?

1.C# supports the struct type and Java does not.

2. Java does not provide operator overloading,

3. C# provides built – in delegates and events. Java uses interfaces and inner classes

to achieve a similar result.

4. C# provides a fourth type of iteration statement, foreach for quick and easy

iterations over collections and array type data.

5. C# checks overflows using checked statements.

6. C# uses is operator instead of instanceof operator in Java.

7. C# allows a variable number of parameters using the params keyword.

8. What are the Microsoft .NET products and services consist of the following:

. Windows .NET. MSN .NET. Office .NET. Visual Studio .NET

. Personal subscription services. bCentral for .NET

9. What is Common Language Runtime (CLR)?

The Common Language Runtime, popularly known as CLR is the heart and soul of the .NET framework. As the name suggests, CLR is a runtime environment in which programs written in C# and other .NET languages are executed. It also supports cross-language interoperability.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 metadata

10. What is Common Type Systems (CTS)?

  • The .NET framework provides multiple language support using the feature known as Common Type Systemthat is built into the CLR.
  • The CTS supports a variety of types and operations found in most programming languages and therefore calling one language from another does not require type conversions.
  • Although C# is specially designed for the .NET platform, we can build .NET programs in a number of other languages including c++ and Visual Basic.

11. What is Common Language Specification (CLS)?

  • The Common Language Specification defines a set of rules that enables interoperability on the .NET platform.
  • These rules serve as a guide to third-party compiler designers and library builders.
  • The CLS is a subset of CTS and therefore the languages supporting the CLS can use each others' class libraries as if they are their own.
  • Application Program Interfaces (APIs) that are designed following the rules of CLS can easily be used by all the .NET languages.

12. What is Microsoft Intermediate Language (MSIL)?

  • MSIL, or simply IL, is an instruction set into which all the .NET programs are compiled.
  • It is akin to assembly language and contains instructions for loading, storing, initializing and calling methods.
  • When we compile a C# program or any program written in a CLS-compliant language, the source code is compiled into MSIL.

13.Briefly explain the JIT (Just – in – Time) compiler.

  • Before MSIL can be executed, it must be converted by a .NET Framework Just – In-Time (JIT) compiler to native code, which is CPU – specific code that runs on the same computer architecture that the JIT compiler is running on.
  • Because the runtime supplies a JIT compiler for each CPU architecture that the runtime operates on, developers can write a set of MSIL that can be JIT – compiled and executed on computers with different architectures.
  • The idea behind JIT compilation recognizes the fact that some code may never get called during execution.
  • As part of compiling MSIL to native code, the code must pass a verification process.
  • Verification examines MSIL and metadata to see whether the code can be determined to be type-safe, which means that it is known to access only the memory locations it is authorized to access.

14. What is anAssembly?

An assembly is the functional unit of sharing and reuse in the Common Language Runtime. It provides the Common Language Runtime with the information it needs to be aware of type implementations.

15. What is Managed Code and Unmanaged Code?

Managed Code

  • The Code is Understandable by CLR.
  • It is executed under the instructions of CLR.
  • The code which is done by .net compatible language will be compiled two times. In the first compilation, the compiler will generate MSIL code. Which is known as Managed Code.

UnManaged Code

  • The CLR cannot able to understand the code.
  • The CLR cannot instruct the code.
  • The second time compilation is unmanaged code. It is understood only by themachine not by the user.

16. What are the framework Base classes?

1. String handling2. Managing arrays, lists, maps, etc

3. Accessing files and file systems4. Accessing the registry

5. Security6. Windowing

7.windows messages8. Database management

9. Evaluation of mathematical functions10. Drawing

11.Managing errors and exceptions12. Connecting to the Internet

13.Input/Output Operations

17. How does DOT.Net achieve Interoperability?

Dot.net achieve interoperability with the help of Common Type System (CTS), every language that run in the .net platform has to obey the minimum set of rules. Each language can interact with each other using the CTS and CLS.

18. What are benefits for the .Net?

  • Simple and faster systems development
  • Rich object model
  • Enhanced built-in functionality
  • Many different ways to communicate with the outside world
  • Integration of different languages into one platform
  • Easy deployment and execution
  • Wide range of scalability

19. What is Command Line Argument?

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 comman line arguments. Command line arguments are parameters supplied to the Main method at the time ( invoking it-for execution.

using system;

class SampleSix

{

public static void Main (string [] args)

{

Console.Write ("welcome to");

Console.Write (" "+args [0]);

Console.WriteLine (" "+args[1]);

}

20. C# has to support multiple main methods. Justify your answer.

We have stated earlier that one of the classes must have the Main method as a member. This is what' I normally expected. However, C# includes a feature that enables us to define more than one class wipI:i the Main method. Since Main is the entry point for program execution, there are now more than 1 entry points. In fact, there should be only one. This problem can be resolved by specifying which is to be used to the compiler at the time of compilation as shown below:

csc filename.cs/main:classname

Filename is the name of the file where the code is stored and classname is the name of the containing the Main which we would like to be the entry point.

The following simple program that has two classes containing Main methods. It in a file called multimain.cs and compile it using the switch

main:ClassA or main:ClassB as required

using System;

class ClassA

{

public static void Main( )

{

Console.WriteLine("Class A");

}

}

class ClassB

{

public static void Main( )

{

Console.WriteLine("Class B");

}

}

21. What are the difference between Value type and Reference type?

  • Where they are stored in the memory
  • How they behave in the context of assignment statements

Value types

Value types (which are of fixed length) are stored on the stack, and when a value of a variable in assigned to another variable, the value is actually copied. This means that two identical copies of the value are available in memory.

Reference types

Reference types (which are of variable length) are stored on the heap, and when an assignment between two reference variable occurs, only the reference is copied; the actual value remains in the same memory location. This means that there are two references to a single value.

22. What is boxing and Unboxing? Give an example.

Since value type such as int and long are not objects we cannot use them to call methods. C# enables us to achieve this through a technique known as boxing. Boxing means the conversion of a value type on the stack to a object type on the heap. Conversely, the conversion from an object type back to a value type is known as unboxing.

Boxing

Any type, value or reference can be assigned to an object without an explicit conversion. When the compiler finds a value type where it needs a reference type, it creates an object ‘box’ into which it places the value of the value type.

int m = 100;

object om = m;

Ex:

int m = 10;

object om = m;

m = 20;

Console.WirteLine (m);// m = 20

Console.WriteLine(om);//om = 10

When a code changes the value of m, the value of om is not affected.

UnBoxing

Unboxing is the process of converting the object type back to the value type.

int m = 10;

object om = m;

int n = (int) om;

When performing unboxing, C# checks that the value type we request is actually stored in the object under conversion. Onlyif it is , the value is unboxed.

When unboxing a value, we have to ensure that the value type is large enough to hold the value of the object. Otherwise, the operation may result in a runtime error. For example, the code

int m = 500;

object om = m;

byte n = (byte) om;will produce a runtime error.

23. List out the operators in C#.

  • Arithmetic operators
  • Relational operators
  • Logical operators
  • Assignment operators
  • Increment and Decrement operators
  • Conditional operators
  • Bitwise operators
  • Special operators

24. What are special operators in C#?

isastypeofsizeofnew . (dot) checked unchecked

25. Write the Syntax for the foreach statement in C#?

The foreach statement is similar to the for statement but implemented differently. It enables us to iterate the elements in arrays and collection classes such as List and Hash Table.

The general form of the foreach statement is:

foreach (type variable in expression)

{

Body of the Loop

}

26. What is the advantages for the foreach statement over the for loop?

The advantage of foreach over the for statement is that it automatically detects the boundaries of the collection being iterated over.

27. How to achieve the fall through statement in C#?

To achieve the fall through statement in C# is to implement to the goto statement in the switch case statement. For Example:

switch(ch)

{

case: 1

Console.WriteLine(“This is A”);

goto 2;

case: 2

Console.WriteLine(“This is B”);

goto 3;

case: 3

Console.WriteLine(“This is C”);

goto default;

default:

Console.WriteLine(“End of the Switch Case”);

}

28. List out the method modifiers.

Modifiers / Description
new / The method hides an inherited method with the same signature.
public / The method can be accessed from anywhere, including outside the class.
protected / The method can be accessed from within the class to which it belongs or a type derived from that class.
internal / The method can be accessed from within the same program.
private / The method can only be accessed from inside the class to which it belongs.
static / The method does not operate on a specific instance of the class.
virtual / The method ca be overridden by a derived class.
abstract / A virtual method which defined the signature of the method, but doesn’t provide an implementation.
override / The method overrides an inherited virtual or abstract method.
sealed / The method overrides an inherited virtual method, but cannot be overridden by override.
extern / The method is implemented externally, in a different language.

29. What are the advantages for the Structures?

  • They are created much more quickly than heap – allocated type.
  • They are instantly and automatically deallocated once they go out of scope.
  • It is easy to copy value type variables on the stack.

30. Define a struct:

A struct 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 createstemplate that may be used to define the data properties. Once the structure type has been defined, we can create variable of that type using declarations that are similar to the built – in type declarations.

Syntax:Example:

struct struct-namestruct Student

{{

data member1;public string name;

data member2;public double totalmarks;

}}

31. List out the differences between Classes and Structures

Category / Classes / Structs
Data Type / Reference type and therefore stored on the heap / Value type and therefore stored on the stack.
Inheritance / Support Inheritance / Do not support inheritance
Default Values / Default value of a class type is null / Default value is the value produced by ‘zeroing out’ the fields of the struct.
Field initialization / Permit initialization of instance fields / Do not permit initialization of instance fields.
Constructors / Permit declaration of paramerterless constructors / Do not permit declaration of parameterless constructor
Destructor / Supported / Not Supported
Assignment / Assignment copies the reference / Assignment copies the values.

32. Define Enumeration.

An Enumeration is a user – defined integer type which provides a way for attaching names to numbers, thereby increasing the comprehensibility of the code. The enum keyword automatically enumerates a list of words by assigning those values 0, 1, 2 and so on. This facility provides an alternative means of creating ‘constant’ variable names.

Syntax:

enum Shape

{

Circle,

Square,

Triangle

}

OR

enum Shape {Circle, Square, Triangle}

33. What is Verbatim Strings?

Verbatim strings are those that start with the @ symbol. This symbol tells the compiler that the string should be used verbatim even if it includes escape characters.

string s1 = @ “\EBG\CSharp\string.cs”;

(or)

string s1 = \\EBG\\CSharp\\string.cs;

34. What is the Usage of Checked Operator in C#?

Checked Operator is used to validate the arithmetic expression to know whether any overflow occur as a result of the given expression. If Overflow occurs its throwsan exception indicating the overflow.

35. Differentiate the String and String Builder type in C#?

String / String Builder
Immutable String type,that we cannot modify the characters that contained in them. / Mutable String type, these types accept the changes.
All operations results in a modified version of a new string / Operations carried out on the original string only.

36. What are the advantages that properties can have over the variables?

Properties combine the fields with the methods that access it. It provides more control over the fields and makes accessible the private variables outside the class using properties.

37. Can we create a copy of an existing array and make the changes without affecting the original array?

Yes, we can create the exact copy of the original array and modify the without changing the original array using the Clone method of array type.

38. How does DOT.Net achieve Interoperability?

Dot.net achieve interoperability with the help of Common Type System(CTS), every language that run in the .net platform has to obey the minimum set of rules. Each language can interact with each other using the CTS and CLS.

39. What are the different ways that a parameter passes to a method?

  • Pass By value
  • Pass by Reference
  • Out parameter and
  • Params array

40. Differentiate int from integer in C#?

In object oriented languages such as Java and C#, the 'int' type is a primitive,whereas the 'Integer' type is an object.Objects provide facilities for polymorphism, are passed by reference, and are allocated from the heap. Conversely, primitives are passed by value and are allocated from the stack.

PART B QUESTIONS

  1. Draw appropriate diagrams and discuss the various components of the .Net platform andarchitecture of the .NET framework.
  2. Explain the Common Language Runtime and its components.
  3. What is Jagged array? Explain its use with simple example.
  4. Explain the different types of method parameter.
  5. Explain The Features of C# Language?
  6. How fall-through is achieved in .Net
  7. Explain the different categories of operators in C#?
  8. Explain the following a) Array b) ArrayListc)foreachd) structures.

UNIT – II

PART A – Two Marks

1. Whatare the access modifiers in C#?

Modifiers / Accessibility Control
private / Member is accessible only within the class containing the member.
public / Member is accessible from anywhere outside the class as well. It is also accessible in derived classes.
protected / Member is visible only to its own class and its derived classes.
internal / Member is available within the assembly or component that is being created but not to the clients of that component.
protected internal / Available in the containing program or assembly and in the derived class.

2. What are the restrictions for the Static methods?