C# (1)
C# (1)
C# is an object oriented, type safe and managed language that is compiled by .Net framework to
generate Microsoft Intermediate Language.
3. Can multiple catch blocks be executed?
No, Multiple catch blocks can’t be executed. Once the proper catch code executed, the control is
transferred to the finally block and then the code that follows the finally block gets executed.
4. What is the difference between public, static and void?
All these are access modifiers in C#. Public declared variables or methods are accessible anywhere
in the application. Static declared variables or methods are globally accessible without creating an
instance of the class. The compiler stores the address of the method as the entry point and uses
this information to begin execution before any objects are created. And Void is a type modifier that
states that the method or variable does not return any value.
5. What is an object?
An object is an instance of a class through which we access the methods of that class. “New”
keyword is used to create an object. A class that creates an object in memory will contain the
information about the methods, variables and behavior of that class.
6. Define Constructors?
A constructor is a member function in a class that has the same name as its class. The constructor
is automatically invoked whenever an object class is created. It constructs the values of data
members while initializing the class.
7. What is Jagged Arrays?
The array which has elements of type array is called jagged array. The elements can be of different
dimensions and sizes. We can also call jagged array as Array of arrays.
8. What is the difference between ref & out parameters?
An argument passed as ref must be initialized before passing to the method whereas out
parameter needs not to be initialized before passing to a method.
9. What is the use of using statement in C#?
The using block is used to obtain a resource and use it and then automatically dispose of when the
execution of block completed.
10. What is serialization?
When we want to transport an object through network then we have to convert the object into a
stream of bytes. The process of converting an object into a stream of bytes is called Serialization.
For an object to be serializable, it should inherit ISerialize Interface.
De-serialization is the reverse process of creating an object from a stream of bytes.
11. Can “this” be used within a static method?
We can’t use ‘This’ in a static method because we can only use static variables/methods in a static
method.
12. What is difference between constants and read-only?
Constant variables are declared and initialized at compile time. The value can’t be changed after
wards. Read-only variables will be initialized only from the Static constructor of the class. Read
only is used only when we want to assign the value at run time.
13. What is an interface class?
Interface is an abstract class which has only public abstract methods and the methods only have
the declaration and not the definition. These abstract methods must be implemented in the
inherited classes.
14. What are value types and reference types?
Value Type:
A Value Type stores its contents in memory allocated on the stack. When you created a Value Type,
a single space in memory is allocated to store the value and that variable directly holds a value. If
you assign it to another variable, the value is copied directly and both variables work
independently. Predefined datatypes, structures, enums are also value types, and work in the
same way. Value types can be created at compile time and Stored in stack memory, because of
this, Garbage collector can't access the stack.
int x = 10;
Reference Type:
Reference Types are used by a reference which holds a reference (address) to the object but not
the object itself. Because reference types represent the address of the variable rather than the
data itself, assigning a reference variable to another doesn't copy the data. Instead it creates a
second copy of the reference, which refers to the same location of the heap as the original value.
Reference Type variables are stored in a different area of memory called the heap. This means that
when a reference type variable is no longer used, it can be marked for garbage collection.
Examples of reference types are Classes, Objects, Arrays, Indexers, Interfaces etc.
e.g.
int[] iArray = new int[20];
In the above code the space required for the 20 integers that make up the array is allocated on the
heap.
34. What is the base class in .net from which all the classes are derived from?
[csharp]System.Object[/csharp]
35. What is the difference between method overriding and method overloading?
Overloading: is the mechanism to have more than one method with same name but with
different signature (parameters). A method can be overloaded on the basis of following properties
1. Have different number of parameter
2. Having same number of parameters but of different type
3. Having same number and type of parameters but in different orders
A method cannot be overloaded on the basis of
1. Different return type
2. Different access modifier
3. Normal and optional parameters
n the same way we can overload constructor of a class by defining more than one constructor with
different parameters which is known as constructor overloading.
Overriding: Overriding can be done in derived class, an override method provides a new
implementation of a method inherited from parent class.
To override a method in base (parent) class it must be
1. virtual
2. abstract
3. override
We cannot override a base method which is in base class as
1. non-virtual
2. static
We cannot use the following modifiers to modify an override method in derived class
1. new
2. static
3. virtual
4. abstract
Conclusion:
Overloading can be done in same class
Overriding can be done in parent and derived class
Overloading in used when we need same method in same class with different parameters
Overriding is used when we need to redefine a method that has already been defined in
parent class (using the exact same signature
Overloading is resolved at compile time
Overriding is resolved at run time
What is a Destructor?
A Destructor has the same name as the class with a tilde character and is used to destroy an
instance of a class.
Usually in .NET, the CLR takes care of memory management. Is there any need for a
programmer to explicitly release memory and resources? If yes, why and how?
If the application is using expensive external resource, it is recommend to explicitly release the
resource before the garbage collector runs and frees the object. We can do this by implementing
the Dispose method from the IDisposable interface that performs the necessary cleanup for the
object. This can considerably improve the performance of the application.
When do we generally use destructors to release resources?
If the application uses unmanaged resources such as windows, files, and network connections, we
use destructors to release resources.
Private
The type or member can only be accessed by code in the same class or struct.
Protected
The type or member can only be accessed by code in the same class or struct, or in a
derived class.
Internal
The type or member can be accessed by any code in the same assembly, but not from
another assembly.
Protected Internal
The type or member can be accessed by any code in the same assembly, or by any
derived class in another assembly.
4.How do we use different versions of private assemblies in same application without re-build?
In Asseblyinfo file need specify assembly version.
assembly: AssemblyVersion - See more at: http://www.aired.in/2009/10/c-realtime-interview-questions-
and.html#sthash.KPLMJ5oB.dpuf