Q. Object V/S var V/S dynamic
The object class is the base class for all ohter classes; in other words all derived types are inherited from the object base type. We can assign values of any type to the object type variable (value type->obejct=boxing) (object->value type=unboxing)
var keyword was introduced in C# 3.0. It is an implicit type and the type is determined by the C# compiler. There is no performance penalty when using var. The C# compiler assigns types to the variable when it assigns the value.
var a = 10; // it is same as int a = 10;
var b = "This is test string";
var c = b; // does not gives compilation error.
var d; //gives compilation error
Use when ->We have a long class name so that our code is not so readable. var makes it short and sweet.
We use LINQ and anonymous types that help us reduce the code (reduce effort to create a new class)
C# 4.0 introduced a new type called "Dynamic". Dynamic is a new static type that is not known until runtime. At compile time, an element with dynamic is assumed to support any operation so that we need not be worried about the object. Errors are caught at runtime only. The type's dynamic status is compiled into a variable type object that is dynamic at compile time, not at run time. At compilation a dynamic is converted into a System.object. It suffers from boxing and unboxing because it is treated as a System.Object and also the application's performance will suffer siince the compiler emits the code for all the type safety.
dynamic i = 10;
dynamic s = "abcd";
Q. Static vs singleton
Ans: Static classes are basically used when you want to store a single instance, data which should be accessed globally throughout your application. The class will be initialized at any time but mostly it is initialized lazily. Lazy initialization means it is initialized at the last possible moment of time. There is a disadvantage of using static classes. You never can change how it behaves after the class is decorated with the static keyword.
Imp->Singleton Class instance can be passed as a parameter to another method whereas static class cannot
->Singleton classes support Interface inheritance whereas a static class cannot implement an interface:
->Static classes also fail during dependency injection.
Q. How to do method overloading in WCF/webservices?
Ans: WSDL does not support the same overloading concepts that are supported by C#. When you are consuming a service over HTTP/SOAP, having the same method name in your contract would mean that there is no way to determine the particular method the client can invoke.
It will throw an error contract mismatch because of the WSDL that does n't allow to create duplicate methods for clients.
[ServiceContract]
public interface IHelloWorld
{
[OperationContract(Name = "ShowIntData")]
string ShowData(int value);
[OperationContract(Name = "ShowStringData")]
string ShowData(string value);
}
So, now you fetch the overloaded methods with different name
----------------------------------------------------------------
Q: Can an abstract class have a constructor? If so what is the use?
Ans : Yes, an abstract class can have a constructor. In general, a class constructor is used to initialize fields. Along the same lines, an abstract class constructor is used to initialise fields of the abstract class. You would provide a constructor for an abstract class if you want to initialise certain fields of the abstract class before the instantiation of a child-class takes place. An abstract class constructor can also be used to execute code that is relevant for every child class. This prevents duplicate code.You cannot create an instance of an abstract class. So, what is the use of a constructor in an abstract class?Though you cannot create an instance of an abstract class, we can create instances of the classes that are derived from the abstract class. So, when an instance of derived class is created, the parent abstract class constructor is automatically called. Note: Abstract classes can't be directly instantiated. The abstract class constructor gets executed thru a derived class. So, it is a good practice to use protected access modifier with abstract class constructor. Using public doesn’t make sense.
----------------------
Q :How to handle exceptions that occur in finally block
Ans: The exception propagates up, and should be handled at a higher level. If the exception is not handled at the higher level, the application crashes. The "finally" block execution stops at the point where the exception is thrown. Irrespective of whether there is an exception or not "finally" block is guaranteed to execute.
1. If the "finally" block is being executed after an exception has occurred in the try block,
2. and if that exception is not handled
3. and if the finally block throws an exception
Then the original exception that occurred in the try block is lost.
-----------------
Q: Throw vs Throw(ex)
Ans: throw(ex) will reset your stack trace so error will appear from the line where throw(ex) written while throw does not reset stack trace and you will get information about original exception.
----------------
Q: Execution order in asp.net
TextBox Init Event
UserControl Init Event
Master Page Init Event
Content Page Init Event
Content Page Load Event
Master Page Load Event
TextBox Load Event
UserControl Load Event
Content Page PreRender Event
Master Page PreRender Event
TextBox PreRender Event
UserControl PreRender Event
So, in general the initialization events are raised from the innermost control to the outermost one, and all other events are raised from the outermost control to the innermost one.Please note that the master page is merged into the content page and treated as a control in the content page. Master page is merged into the content page during the initialization stage of page processing.
The object class is the base class for all ohter classes; in other words all derived types are inherited from the object base type. We can assign values of any type to the object type variable (value type->obejct=boxing) (object->value type=unboxing)
var keyword was introduced in C# 3.0. It is an implicit type and the type is determined by the C# compiler. There is no performance penalty when using var. The C# compiler assigns types to the variable when it assigns the value.
var a = 10; // it is same as int a = 10;
var b = "This is test string";
var c = b; // does not gives compilation error.
var d; //gives compilation error
Use when ->We have a long class name so that our code is not so readable. var makes it short and sweet.
We use LINQ and anonymous types that help us reduce the code (reduce effort to create a new class)
C# 4.0 introduced a new type called "Dynamic". Dynamic is a new static type that is not known until runtime. At compile time, an element with dynamic is assumed to support any operation so that we need not be worried about the object. Errors are caught at runtime only. The type's dynamic status is compiled into a variable type object that is dynamic at compile time, not at run time. At compilation a dynamic is converted into a System.object. It suffers from boxing and unboxing because it is treated as a System.Object and also the application's performance will suffer siince the compiler emits the code for all the type safety.
dynamic i = 10;
dynamic s = "abcd";
Q. Static vs singleton
Ans: Static classes are basically used when you want to store a single instance, data which should be accessed globally throughout your application. The class will be initialized at any time but mostly it is initialized lazily. Lazy initialization means it is initialized at the last possible moment of time. There is a disadvantage of using static classes. You never can change how it behaves after the class is decorated with the static keyword.
Imp->Singleton Class instance can be passed as a parameter to another method whereas static class cannot
->Singleton classes support Interface inheritance whereas a static class cannot implement an interface:
->Static classes also fail during dependency injection.
Q. How to do method overloading in WCF/webservices?
Ans: WSDL does not support the same overloading concepts that are supported by C#. When you are consuming a service over HTTP/SOAP, having the same method name in your contract would mean that there is no way to determine the particular method the client can invoke.
It will throw an error contract mismatch because of the WSDL that does n't allow to create duplicate methods for clients.
[ServiceContract]
public interface IHelloWorld
{
[OperationContract(Name = "ShowIntData")]
string ShowData(int value);
[OperationContract(Name = "ShowStringData")]
string ShowData(string value);
}
So, now you fetch the overloaded methods with different name
----------------------------------------------------------------
Q: Can an abstract class have a constructor? If so what is the use?
Ans : Yes, an abstract class can have a constructor. In general, a class constructor is used to initialize fields. Along the same lines, an abstract class constructor is used to initialise fields of the abstract class. You would provide a constructor for an abstract class if you want to initialise certain fields of the abstract class before the instantiation of a child-class takes place. An abstract class constructor can also be used to execute code that is relevant for every child class. This prevents duplicate code.You cannot create an instance of an abstract class. So, what is the use of a constructor in an abstract class?Though you cannot create an instance of an abstract class, we can create instances of the classes that are derived from the abstract class. So, when an instance of derived class is created, the parent abstract class constructor is automatically called. Note: Abstract classes can't be directly instantiated. The abstract class constructor gets executed thru a derived class. So, it is a good practice to use protected access modifier with abstract class constructor. Using public doesn’t make sense.
----------------------
Q :How to handle exceptions that occur in finally block
Ans: The exception propagates up, and should be handled at a higher level. If the exception is not handled at the higher level, the application crashes. The "finally" block execution stops at the point where the exception is thrown. Irrespective of whether there is an exception or not "finally" block is guaranteed to execute.
1. If the "finally" block is being executed after an exception has occurred in the try block,
2. and if that exception is not handled
3. and if the finally block throws an exception
Then the original exception that occurred in the try block is lost.
-----------------
Q: Throw vs Throw(ex)
Ans: throw(ex) will reset your stack trace so error will appear from the line where throw(ex) written while throw does not reset stack trace and you will get information about original exception.
----------------
Q: Execution order in asp.net
TextBox Init Event
UserControl Init Event
Master Page Init Event
Content Page Init Event
Content Page Load Event
Master Page Load Event
TextBox Load Event
UserControl Load Event
Content Page PreRender Event
Master Page PreRender Event
TextBox PreRender Event
UserControl PreRender Event
So, in general the initialization events are raised from the innermost control to the outermost one, and all other events are raised from the outermost control to the innermost one.Please note that the master page is merged into the content page and treated as a control in the content page. Master page is merged into the content page during the initialization stage of page processing.
No comments:
Post a Comment