Posts

Showing posts with the label C#

Method Parameter in C#

C# is having 4 parameter types which are: Value Parameter : Default parameter types.Only Input Reference (ref) Parameter : Input / Output Output (out) Parameter  Parameter (params) Arrays Value Parameter      Are used for passing parameters into methods by value.When a method is invoked, the values of actual parameter are assigned to the corresponding formal parameter. The values can be changed with in the method. The value of the actual parameter that is passed by value to a method is not changed made to the corresponding formal parameter within of the method. This is because the methods refer to only copies of those variables when they are passed by value. Reference Parameter (Pass By Reference)      Used to pass parameters, a reference parameter does not create a new storage location. Instead it represents the same storage location as the actual parameter used in the method invocation. Reference parameter is used in situations where...

Boxing & Unboxing in C#

Boxing is an implicit conversion of a value type to the type object.                       int i = 123;  // A value type                       object box = i; // Boxing Unboxing is an explicit conversion from the type object to a variable type.                       int i = 123; // A value type                       object box = i; // Boxing                       int j = (int)box; // Unboxing Casting: casting is the process of converting a variable from one type to another( from a string to an integer)  In a simple language we can define: Boxing -  converting   value type to reference type  Unboxing - converting reference type to value type. Hope this will make you understan...

Extension Class in C#

Extension method is a new feature in C# 3.0.  Extension methods allow existing classes to be extended without relying on inheritance or having to change the class's source code. This means that if you want to add some methods into the existing String class you can do it quite easily. Here's a couple of rules to consider when deciding on whether or not to use extension methods:   Extension methods cannot be used to override existing methods. An extension method with the same name and signature as an instance method will not be called.     The concept of extension methods cannot be applied to fields, properties or events. Use extension methods sparingly....overuse can be a bad thing!

Anonymous type in C#

This is a new feature in C# 3.0. This enable use to create a type/class on-the-fly at compile time.  For example: var emp = new { Name = "Sheo", Gender = "Male", Active = true }; In this case a new type will be created on the fly that will have Name, Gender, and Active as public property and its backing field like _Name, _Gender, _Active. This is especially useful when we want to receive data from other object or iterate through a collection and set values and do not want to create a class just to hold the data.  Note that: anonymous types are just a placeholder, we can't customize its behavior or add methods inside it

Difference between Shadow and Override

Most of the time these question(difference's) are asked in interview's. That's why i thought to discuss this with you people. Shadowing: - This is a VB.Net Concept by which you can provide a new implementation for the base class member without overriding the member. You can shadow a base class member in the derived class by using the keyword "Shadows". The method signature , access level and return type of the shadowed member can be completely different than the base class member. Hiding: - This is a C# Concept by which you can provide a new implementation for the base class member without overriding the member. You can hide a base class member in the derived class by using the keyword "new". The method signature, access level and return type of the hidden member has to be same as the base class member. Comparing the three:- The access level, signature and the return type can only be changed when you are shadowing with VB.NET. Hiding a...

Difference between const and readonly

The read only keyword is different from the const keyword. A const field can only be initialized at the declaration of the field. A read only field can be initialized either at the declaration or in a constructor. Therefore read only field can have different values depending on the constructor used. Also, while a const field is a compile time constant but the read only field can be used for run time constants.