C# (Sharp) Programming Language

  Home  Microsoft .Net Technologies  C# (Sharp) Programming Language


“Learn C# (Sharp) Programming Language by Interview Questions and Answers”



163 C# (Sharp) Programming Language Questions And Answers

101⟩ If a base class has a bunch of overloaded constructors ...

If a base class has a bunch of overloaded constructors, and an inherited class has another bunch of overloaded constructors, can you enforce a call from an inherited constructor to an arbitrary base constructor?

Yes, just place a colon, and then keyword base (parameter list to invoke the appropriate constructor) in the overloaded constructor definition inside the inherited class.

 223 views

105⟩ What is the C# equivalent of C++ catch (…), which was a catch-all statement for any possible exception? Does C# support try-catch-finally blocks?

Yes. Try-catch-finally blocks are supported by the C# compiler. Here's an example of a try-catch-finally block: using System;

public class TryTest

{

static void Main()

{

try

{

Console.WriteLine("In Try block");

throw new ArgumentException();

}

catch(ArgumentException n1)

{

Console.WriteLine("Catch Block");

}

finally

{

Console.WriteLine("Finally Block");

}

}

}

Output: In Try Block

Catch Block

Finally Block

If I return out of a try/finally in C#, does the code in the finally-clause run? Yes. The code in the finally always runs. If you return out of the try block, or even if you do a "goto" out of the try, the finally block always runs, as shown in the following

example: using System;

class main

{

public static void Main()

{

try

{

Console.WriteLine("In Try block");

return;

}

finally

{

Console.WriteLine("In Finally block");

}

}

}

Both "In Try block" and "In Finally block" will be displayed. Whether the return is in the try block or after the try-finally block, performance is not affected either way. The compiler treats it as if the return were outside the try block anyway. If it's a return without an expression (as it is above), the IL emitted is identical whether the return is inside or outside of the try. If the return has an expression, there's an extra store/load of the value of the expression (since it has to be computed within the try block).

 223 views

107⟩ Is there a way to force garbage collection?

Yes. Set all references to null and then call System.GC.Collect(). If you need to have some objects destructed, and System.GC.Collect() doesn't seem to be doing it for you, you can force finalizers to be run by setting all the references to the object to null and then calling System.GC.RunFinalizers().

 226 views

108⟩ Does C# support properties of array types?

Yes. Here's a simple example: using System;

class Class1

{

private string[] MyField;

public string[] MyProperty

{

get { return MyField; }

set { MyField = value; }

}

}

class MainClass

{

public static int Main(string[] args)

{

Class1 c = new Class1();

string[] arr = new string[] {"apple", "banana"};

c.MyProperty = arr;

Console.WriteLine(c.MyProperty[0]); // "apple"

return 0;

}

}

 202 views

110⟩ What is a satellite assembly?

When you write a multilingual or multi-cultural application in .NET, and want to distribute the core application separately from the localized modules, the localized assemblies that modify the core application are called satellite assemblies.

 207 views

115⟩ How do I register my code for use by classic COM clients?

Use the regasm.exe utility to generate a type library (if needed) and the necessary entries in the Windows Registry to make a class available to classic COM clients. Once a class is registered in the Windows Registry with regasm.exe, a COM client can use the class as though it were a COM class.

 171 views

116⟩ How do I do implement a trace and assert?

Use a conditional attribute on the method, as shown below:

class Debug

{

[conditional("TRACE")]

public void Trace(string s)

{

Console.WriteLine(s);

}

}

class MyClass

{

public static void Main()

{

Debug.Trace("hello");

}

}

In this example, the call to Debug.Trace() is made only if the preprocessor symbol TRACE is defined at the call site. You can define preprocessor symbols on the command line by using the /D switch. The restriction on conditional methods is that they must have void return type.

 203 views

117⟩ How do I create a multilanguage, multifile assembly?

Unfortunately, this is currently not supported in the IDE. To do this from the command line, you must compile your projects into netmodules (/target:module on the C# compiler), and then use the command line tool al.exe (alink) to link these netmodules together.

 217 views

119⟩ C# provides a default constructor for me. I write a constructor that takes a string as a parameter ...

C# provides a default constructor for me. I write a constructor that takes a string as a parameter, but want to keep the no parameter one. How many constructors should I write?

Two. Once you write at least one constructor, C# cancels the freebie constructor, and now you have to write one yourself, even if there is no implementation in

 217 views