Home
C-Sharp
Interview FAQs

Show

FAQs of level

The partial keyword allows the class, struct, or interface to span across multiple files. Typically a class resides entirely in a single file. However, in situations where multiple developers need access to the same class, or morelikely in the situation where a code generator of some type is generating part of a class, then having the class in multiple files can be beneficial.

The way that the partial keyword is used is to simply place partial keyword before the class, struct, or interface. Eg.

//BigClass1.cs
partial class TheBigClass
{
public void method1(){ }
}

//BigClass2.cs
partial class TheBigClass
{
public void method2(){ }
}

When the project that these two source files are part of is compiled, a single type called TheBigClass will be created with two methods.
If a class contains nothing but static methods and properties, the class itself can become static. A static class is functionally the same as creating a class with a private static constructor. An instance of the class can never be created. By using the static keyword, the compiler can help by checking that instance member are never accidentally added to the class. If they are, a compiler error happens. This can help guarantee that an instance is never created. The syntax for static class looks like :

static class StaticUtilities {
public static void HelperMethod(){ }
}

An object of type StaticUtilities is not needed to call the HelperMethod(). The type name is used to make the call:

StaticUtilities.HelperMethod();
Shadowing is a concept of polymorphism usage in Object Oriented Programming. This is a concept related to over-riding functions at run-time or making a shadow of the object methods in the inherited classes. Though it is not much used in programming but it may be used sometimes and its usage is very restrictive and unique.
We know that all objects that are created on a class must be given initial values. This can be done in three ways -
1. Using the dot operator to access the instance variables and then assigns values to them individually.
2. Take the help of a function like GetData to initialize each object individually using statement like : rect1.GetData(5, 8);
3. Using constructors.

Constructors are special type of methods, that enables an object to initialize itself when it is created.
Constructors have the same name as the class itself. Secondly, they do not specyfy a return type, not even void. This because they do not return any value.

Constructors are usually public because they are provided to create objects. However they can also be declared as private or protected.
A destructor is opposite to s constructor. It is called when an object is no more required. The name of the destructor is the same as the class name and is preceded by a tilde(~) sign. Like constructors, a destructor has no return type.

C# manages the memory dynamically and uses a garbage collector, running on a separate thread, to execute all destructors on exit. The process of calling a destructor when an object is reclaimed by the garbage collector is called finalization.

C# supports two predefined reference types :
1. object (System.Object) - The root type, from which all other types in the CTS derive (Including Value Types)
2. string (System.String) - Unicode character string.
One very important aspect of IL is that it is based on exceptionally Strong Data Typing. That means that all variables are clearly marked as being of a particular, specific data type (There is no room in IL, for example Variant data type recognized by Visual Basic and scripting languages). In particular, IL does not normally permit any operations that result in ambiguous data types.
Developers develop their application first for functionality and then, they re-work their application to make them more manageable and more readable. This process is referred to as refactoring. Refactoring is the process of reworking code for more readability, performance, providing type safety, and lining application up to better adhere to OO programming practices.
C# 2.0 provides a new feature called Anonymous Methods, which allow you to create inline un-named ( i.e. anonymous ) methods in your code, which can help increase the readability and maintainability of your applications by keeping the caller of the method and the method itself as close to one another as possible. This is akin to the best practice of keeping the declaration of a variable, for example, as close to the code that uses it.
Prefixing a variable with the const keyword when it is declared and initialized designates that variable as a constant. As the name implies, a constant is a variable whose value cannot be changed during it lifetime:

const int a = 501; // this value cannot be changed.

constants have type following characteristics:
1. They must be initialized when they are declared, and once a value has been assigned, it can never be overwritten.
2. The value of the constant must be computable at the compile time. Therefore you cannot initialize a constant with a value taken from a variable. if need to do this, you will need to use read only fields.
3. Constant are always implicitly static, however, you don not have to include the static modifier in the constant declaration.

PreviousDisplaying 11 of 25Next
Need Help? Contact Us.

Log in

*
*

Forgot password?

*

New User

*
*
*
*