22 January 2013

C#.net Interview Question with Answers -II

Dispose vs Final?
Dispose is a method for realse from the memory for an object.
Finalize is used to fire when the object is going to realize the memory.We can set a alert message to says that this object is going to dispose.

Types of Error?
Configuration Errors.
Parse Errors.
Compilation Errors.
Run-Time Errors.

DataReader vs Dataset?
Data Reader is connected, read only forward only record set.
Dataset is in memory database that can store multiple tables, relations and constraints; moreover dataset is disconnected and is not aware of the

data source.

What is the use of var keyword in C#?
This is the new feature in C# 3.0. This enable us to declare a variable whose type is implicitly inferred from the expression used to initialize the

var age = 10;
Because the initialization value (10) of the variable age is integer type of age will be treated as integer type of variable. There are few limitation of

the var type of variables.
They are:
 They can't be initialized as null.
 They need to be declared and initialized in the same statement.
 They can't be used as a member of the class.

Features of C# 3.0:
Implicitly typed variables (var):C# 3.0 supports implicitly typed local variables by making use of a new keyword var. These variables are not

typeless; rather, their type is inferred at compile time.
Auto properties:Auto-implemented properties are another helpful new feature to save us some typing and reduce the potential to introduce bugs.
Object initialization:C# 3.0 introduces two new features that help when instantiating and initializing object instances. Using object and collection

initializers, you can instantiate and initialize either an object or a collection in one compound statement.
Partial Methods :C# 2.0 introduced partial class definitions.C# 3.0 adds to that by introducing partial methods. Using partial methods, We can

implement the methods in separate file (same partial class) .
Extension Methods:Extension methods are one of the most exciting new features.Extension methods enable you to add methods to existing types

without creating a new derived type.
LINQ (Language Integrated Query):The granddaddy of all new C# 3.0 features has to be LINQ, which builds upon all of the new features,

especially extension methods, lambda expressions, and anonymous types.You can use LINQ to access data from multiple sources. That means

from native object collections, SQL data stores, and XML. 

C# 2.0—Generics (.NET Framework support was added, and C# benefited from this); iterator pattern (the yield keyword); anonymous methods

(the delegate keyword), nullable types, and the null coalescing operator .
 C# 3.0—Anonymous types, extension methods, object initializers,collection initializers, implicitly typed local variables (var keyword),lambda

expressions (=>), and the LINQ query expression pattern.
 C# 4.0—Optional Parameters and Named Arguments, Dynamic typing (dynamic type), improved COM-Interop, and Contra and Co-Variance.

Covariance vs Contravariance:
Convariance : Convert from Wider(Double) to Narrow(Float).
Contravariance: Convert from Narrow(Float) to Wider(Double).

Sealed Class:
The sealed modifier is used to prevent derivation from a class. A compile-time error occurs if a sealed class is specified as the base class of another

class. (A sealed class cannot also be an abstract class).
static vs Readonly?
Static: A variable that retains the same data throughout the execution of a program.
ReadOnly: you can’t change its value.

String vs StringBuilder?
String Class
Once the string object is created, its length and content cannot be modified.
Even after object is created, it can be able to modify length and content.

Parse vs TryParse?
Parse method is used to parse any value to specified data type.
TryParse is a good method if the string you are converting to an interger is not always numeric. The TryParse method returns a boolean to denote

whether the conversion has been successfull or not, and returns the converted value through an out parameter.
Optional and Named Parameter in C# 4.0
Optional parameter allows omitting arguments to function while named parameters allow passing arguments by parameter name.
Anonymous methods:
Anonymous methods are another way to declare delegates with inline code except named methods.

Var vs Dynamic?
Var word was introduced with C#3.5(specifically for LINQ) while dynamic is introduced in C#4.0.
 Variables declared with var keyword will get compiled and you can have all its related methods by intellisense while variables declared with

dynamic keyword will never get compiled. All the exceptions related to dynamic type variables can only be caught at runtime.
Unsafe Keyword:
In C# the value can be directly referenced to a variable, so there is no need of pointer. Use of pointer sometime crashes the application. But C#

supports pointer, that means we can use pointer in C#.The use of pointer in C# is defined as a unsafe code. So if we want to use pointer in C# the

pointer must be present in the body of unsafe declaration. But pointer does not come under garbage collection.
Delegate Keyword derived from which namespace?
C# delegate keyword is derived form System.MulticastDelegate.
Difference Preprocessor directives:
#region , #endregion :- Used to mark sections of code that can be collapsed.
#define , #undef :-Used to define and undefine conditional compilation symbols.
#if , #elif , #else , #endif :- These are used to conditionally skip sections of source code.
Stackalloc Keyword:
In an unsafe context it is used to allocate C# array directly on the stack.
Whick keyword is used for Temporarily fixa variable ?
fixed Keyword.

what is use of ??
This operator allows you to assign a value to a nullable type if the retrieved value is in fact null.
Define Property
Property  acts as a cross link between the field and the method . Actually it behaves as a field. We can retrieve and store data from the field using property.
Define Param Keyword?
In C# param parameter allows us to create a method that may be sent to a set of identically typed arguments as a single logical parameter.
Syntax for Nullable Variable?
Nullable myval=null;
How can we check a nullable variable is assigned?
using hasvalue method.
Virtual Method?
 used to declare a method inside the base class that can be overridden.
1. Virtual method can not be declared as Private
2. Virtual method can not be declared as Static
3. Virtual method may or may not be overridden.
4. In order to override the virtual method of the base class, Override keyword is used provided the method signature of the derived class is same as

base class, even the access modifier should be same.
5. A Virtual method must contain the method body otherwise compiler shall throw error.
6. Virtual keyword is only used in base and derived class scenario where you want your method over-ridable in the derived class.

Ref vs Out Parameters?
ref parameter:
Ref is keyword.
it must be used along with actual and formal arguments.
ref variables must be initialized before passing to a function
out parameter:
It is very similar to call by Reference.
Out is a Keyword.
Which must be used along with actual and formal arguments.
Out variables are not require to initialize, the initialized value will not be passed, only reference will be passed.
Define Generics
Generics means parametrized types. A class, structure, interface, delegates that operates on a parametrized type is called generics.
Types of windows services?
There are two types of windows services. Those are Win32OwnProcess , Win32ShareProcess .
a. Win32OwnProcess: It is a Win32 process that can be started by the Service Controller. This type of Win32 service runs in a process by itself.+
b. Win32ShareProcess: It is a Win32 service that allows sharing of processes with other Win32 services.
When Garbage Collector run?
 Garbage Collector runs :
1. When the system has low physical memory
2. Acceptable memory heap is increased.
3. GC.Collect method is called.
where Shared Assemblies stored?
The shared assemblies are stored in Global assembly cache.
Debug Writer vs Trace.Writer
Debug.Write will work while the application is in both Debug Mode and Release Mode. This is normally used while you are going to debug a  project. This will not be work when you will define some debug points to your project.Trace.write will work while the application is only in Release Mode. This is used in released version of an application. This will compiled when you will define debug points in your project.
Virtual,sealed,override,abstract keyword?
The use of virtual keyword is to enable or to allow a class to be overridden in the derived class.
The use of sealed keyword is to prevent the class from overridden i.e. you can’t inherit sealed classes.
The use of override keyword is to override the virtual method in the derived class.
The use of abstract keyword is to modify the class, method, and property declaration. You cannot directly make calls to an abstract method and

you cannot instantiate an abstract class.
Define Structure
• A struct is a value type.
• The structs doesn’t support inheritance other than implicitly deriving from object.
• A struct can have the members like parameterless constructor, a finalizer, virtual members etc.
Define enum?
A enum is nothing but a special value type which specifies a group of named numeric constants.

Static vs Constant
Static Variable:
1.Variable set at run time
2.Can be assigned for reference types.
3.Initialized on constructors
1.Variable set at compile time itself.
2.Assigned for value types only
3.must be initialized at declaration time
4.only primitive data types.

WindowsDefaultLocation vs WindowsDefaultBounds
WindowsDefaultLocation makes the form to start up at a location selected by the operating system, but with internally specified size.
WindowsDefaultBounds delegates both size and starting position choices to the operating system.
String vs string
1.String is an class(System.String)
2.String is an Reference type(class)

1.string is an alias name of String class that is created by microsoft
2.string is an value type(data type)
3.string is a C# keyword
4.string is a compiler shortcut for System.String class

10 January 2013

Class Diagram in C#.Net

                        Class Diagrams are newly added in Visual Studio 2005 edition. They provide designers, architects and developers a graphical interface to manipulate methods, properties, fields, events for data structures like classes, interfaces, structure etc.
Let us now explore all the features of Class designer.
Creating Classes using Class Diagrams
Create New Project in Visual Studio 2008 called “Class Diagram”.
Add Class Diagram’s File in your Project. It is shown below.

Class Diagram name is Called “Maths.cd”.

Right Click on the Screen .Context menu will display

Click “Class” Menu in the Screen.

Add Class name “Calculation”.
You can also choose Access of Class using Access Drop down box.
Right Click on your Class File in the Class Diagram.
Click “Add” Menu
Click “Property” Menu if you want.  i.e:  first Name, secondName.

Note: Same Procedure for Method, struct, Field, Event.

Here, I create an Add Method for this Program.

Add Parameter for Method:

        Go to Class Details window.

        Add your parameter for your method.

        Two parameters assigned for Add method (firstValue, secondValue).

       Go to method property in the classdiagram file.

       Change the Value of false to true of the static property of the method.

       See below screen

 Go to your Calculation.cs file and add this code in the Add Method

   returnfirstValue + secondValue;
Copy and paste this code in your form_load Event
          int result = Calculation.Add(50,50);
 Thanks for reading this article. 

Today's Quoto:
        Life is like riding a bicycle. To keep your balance, you must keep moving!