Wednesday, 16 March 2011

Data-types in C#

Data types in c# is divided into how many parts?

There are 2 types of Data types in c#:
a. Value type
b. Reference type

Examples of Value type data type?


Example of Reference type data types?


What are Value types and Reference types ?

Value types directly contain their data are either allocated on the stack or allocated in-line in a structure.
Reference types store  a reference to the value's memory address, and are allocated on the heap.

Value and Reference type datatypes are further classified into?

a. User Defined
b. Pre Defined

Eg of user defined data types are:

Eg of Pre defined data types are:

Explain the difference between value types and reference types?

Value Type:
a.    Stores the data.
b.    The value of value types is stored on the managed stack.
c.    One variable can have just one value.
d.    They are lighter objects.
Reference Type:
a.    Stores the reference to the data.
b.    A reference type is allocated on the heap.
c.    several variables can reference the same data
d.    They are heavier objects.

Using ref and out keywords in c#

what is the use of ref and out keyword in C#?

When we pass value of value type or implicit data type to a method we actually pass
a copy of data. so any changes or modification done in the method in data is not reflected

Consider an example:

class refoutsample
    public static void Main(string args[])
        int a =3;
        Console.WriteLine("Value of a is" + a);
    public static void add(int i)

Output of this program is "value of a is 3" though the value is incremented by 1 in the method.
so to avoid this ref or out keyword is used.

P{rogram using ref keyword:

class refoutsample
    public static void Main(string args[])
        int a =3;
        add(ref a);
        Console.WriteLine("Value of a is" + a);
    public static void add(ref int i)

now the output is 4.

Diffrence between ref and out keyword?

In case of ref, value must be initialized before passing it to method whereas in case of out keyword its not necessary.


Tuesday, 15 March 2011

Exception Interview Questions

What’s difference between System exceptions and Application exceptions?

a. All exception derives from Exception Base class i.e. System.Exception class. Exceptions can be generated
programmatically or can be generated by system.
Application Exception serves as the base class for all application-specific exception classes. It derives from
Exception but does not provide any extended functionality. You should derive your custom application exceptions
from Application Exception.

b. Application exception are used when we want to define user defined exception. While system exception are all
which are defined by .NET.

Will the finally block get executed if an exception has not occurred?

What’s the C# syntax to catch any possible exception?
A catch block that catches the exception of type System.Exception. You can also omit the parameter data type in this case and just write catch {}.

Can multiple catch blocks be executed for a single try statement?

No. Once the proper catch block processed, control is transferred to the finally block .

If we write a goto  or a return statement in try and catch block will the finally block execute ?

The code in the finally always runs even if  there are statements like goto or a return statements.

Assembly interview Questions

What is Assembly?
Assembly is the building blocks of the .NET framework. They are the logical grouping of the functionality in a physical file.

What are the different components of Assembly?

Following are the main components of assembly:

1. Manifest : It contains assembly information like Assembly version, security, reference to other assemblies.
2. Type Metadata : Provides information about types defined in assembly like classes, interface, methods, properties.
3. MSIL Code : It is CPU independent set of instruction that can be efficiently converted into native code.
4. Resources  : Resources like .jpg and .bmp files are stored.

Which tool is used to deploy an assembly, so as the assembly is available to all the application?

The GacUtil.exe is the tool, which allows us to add any assembly to the windows GAC (Global Assembly Catche).

What is Namespace?

It is logical grouping of all the classes and methods inside it.

What is Difference between NameSpace and Assembly?

a. Assembly is physical grouping of  logical units whereas Namespace is the logically grouping of classes.
b. Namespace can span multiple assembly.

Where the version information of assembly is stored?

Version information is stored in assembly in manifest.

Is versioning applicable to private assemblies?

No, Versioning concept is only applicable to global assembly cache (GAC) as private assembly lie in there individual folders.

What is concept of strong names ? Twist :- How do we generate strong names  or what is the process of generating strong names?

Strong Name is only needed when  we need to deploy assembly in GAC.Strong Names helps GAC to differentiate between two versions.Strong names use public key cryptography (PKC) to ensure that no one can spoof it.PKC use public key and private key concept.

Following are the step to generate a strong name and sign a assembly :-
a. Go to “Visual Studio Command Prompt”.
b. After you are in command prompt type sn.exe -k “c:\test.snk”.
c. Add the strong name in AssemblyInfo.cs page of the project.
  [assembly: AssemblyKeyFile(@"e:\mykey.snk")]
d. Add the assembly to Cache: in command prompt  type:
gacutil/i- path of assembly

path of assembly = path of the dll file from the bin/debug folder of the project
What is the satellite assemblies?

In a multilingual or multi-cultural application, the localized assemblies, which contain language specific instructions that modify the core application, are kept separately and they are called satellite assemblies.