Home
C-Sharp
Interview FAQs

Show

FAQs of level

Namespace provides a way of organizing related calsses and other types. Unlike the files or a component, a namespace is a logical rather than physical grouping.

When you define a calss in C# file, you can include it within a namespace definition.

The partial keyword allows the class, struct, or interface to span across multiple files. Typically in C# a class will reside entirely in a single file. However in situations where multiple developers need acees to the same class, or more likely in the situation where a code generator of some type generating part of the class, then having a class in multiple files can be beneficial.

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

If a class contains nothing but static methods and properties, the class itself 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 members are never accedentally added to the class. If they are added, the compiler error will happens. This can help guarantee that an instance is never created.

The syntax for static class looks like this

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

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

StaticUtilities.HelperMethod();

No, constructors are not inherited.
The two give identical results, except where the string is null. Convert.ToInt32(null) returns zero, whereas Int32.Parse(null) throws an ArgumentNullException.
This is a two stage process. Firstly, you need to get a Type reference for the type. If the type you want to create an instance of is in either mscorlib or the current assembly, you can just use Type.GetType(name). If it is in a different assemby, you could either call Type.GetType and pass in the full type name including assembly information, or you could find or load the assemnbly and then call Assembly.GetType(name) on that assembly reference.

Once you have got a Type reference, you can either use Activator.CreateInstance(type) to create an instance, or call Type.GetConstructor to get a specific constructor which you can then use to create an instance by calling Invoke on it.

FileInfo fi = new FileInfo(FileUpload1.FileName);
MessageBox.Show("File extention : " + fi.Extension.ToString());
The "is" operator allows you to check whether an object is compatible with a specific type.

For example to check whether a variable is compatible with object type -
int i = 10;
if(i is object)
{
Console.WriteLine("i is an object");
}

int, like all C# data types, inherits from object class; therefore the expresion "i is object" evaluates to true.

The "as" operator is used to perform explicit type conversion of reference type. If the type being converted is compatible with specified type, conversion is performed successfully. However if the types are incompatible the "as" operator returns the value null.

object o1 = "some string";
object o2 = 5;

string s1 = o1 as string; // s1 = "some string"
string s2 = o2 as string; // s2 = null

You can determine the size (in bytes) required on the stack by a value type using the "sizeof" operator

unsafe
{
Console.WriteLine(sizeof(int));
}

It will return the number 4, as an int is four bytes long in C#.

PreviousDisplaying 3 of 25Next
Need Help? Contact Us.

Log in

*
*

Forgot password?

*

New User

*
*
*
*