Set - 1

Question 1 :

What's C# ?

Answer :

C# (pronounced C-sharp) is a new object oriented language from Microsoft and is derived from C and C++. It also borrows a lot of concepts from Java too including garbage collection.

Question 2 :

Is it possible to inline assembly or IL in C# code?

Answer :


Question 3 :

Is it possible to have different access modifiers on the get/set methods of a property?

Answer :

No. The access modifier on a property applies to both its get and set accessors. What you need to do if you want them to be different is make the property read-only (by only providing a get accessor) and create a private/internal set method that is separate from the property.

Question 4 :

Is it possible to have a static indexer in C#? allowed in C#.

Answer :

No. Static indexers are not

Question 5 :

If I return out of a try/finally in C#, does the code in the finally-clause run?

Answer :

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:

using System; 
class main{
	public static void Main(){
			Console.WriteLine(\"In Try block\");
			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).

Question 6 :

I was trying to use an out int parameter in one of my functions. How should I declare the variable that I am passing to it?

Answer :

You should declare the variable as an int, but when you pass it in you must specify it as 'out', like the following: int i; foo(out i); where foo is declared as follows:

[return-type] foo(out int o) { }


Question 7 :

How does one compare strings in C#?

Answer :

In the past, you had to call .ToString() on the strings when using the == or != operators to compare the strings' values. That will still work, but the C# compiler now automatically compares the values instead of the references when the == or != operators are used on string types. If you actually do want to compare references, it can be done as follows: if ((object) str1 == (object) str2) { } Here's an example showing how string compares work:

using System; 
public class StringTest{
	public static void Main(string[] args){
		Object nullObj = null; Object realObj = new StringTest();
		int i = 10;
		Console.WriteLine(\"Null Object is [\" + nullObj + \"]\n\"+ \"Real Object is [\" + realObj + \"]\n\"+ \"i is [\" + i + \"]\n\");
		// Show string equality operators
		string str1 = \"foo\";
		string str2 = \"bar\";
		string str3 = \"bar\";
		Console.WriteLine(\"{0} == {1} ? {2}\", str1, str2, str1 == str2 );
		Console.WriteLine(\"{0} == {1} ? {2}\", str2, str3, str2 == str3 );


Null Object is []
Real Object is [StringTest]
i is [10]
foo == bar ? False
bar == bar ? True

Question 8 :

How do you specify a custom attribute for the entire assembly (rather than for a class)?

Answer :

Global attributes must appear after any top-level using clauses and before the first type or namespace declarations. An example of this is as follows:

using System;
[assembly : MyAttributeClass] class X {}

Note that in an IDE-created project, by convention, these attributes are placed in AssemblyInfo.cs.

Question 9 :

How do you mark a method obsolete?

Answer :

[Obsolete] public int Foo() {...}


[Obsolete(\"This is a message describing why this method is obsolete\")] public int Foo() {...}

Note: The O in Obsolete is always capitalized.

Question 10 :

How do you implement thread synchronization (Object.Wait, Notify,and CriticalSection) in C#?

Answer :

You want the lock statement, which is the same as Monitor Enter/Exit:

lock(obj) { // code }

translates to

try {
	// code


Question 11 :

How do you directly call a native function exported from a DLL? 

Answer :

Here's a quick example of the DllImport attribute in action:

using System.Runtime.InteropServices; \
class C{
	public static extern int MessageBoxA(int h, string m, string c, int type);
	public static int Main(){
		return MessageBoxA(0, \"Hello World!\", \"Caption\", 0);

This example shows the minimum requirements for declaring a C# method that is implemented in a native DLL. The method C.MessageBoxA() is declared with the static and external modifiers, and has the DllImport attribute, which tells the compiler that the implementation comes from the user32.dll, using the default name of MessageBoxA. For more information, look at the Platform Invoke tutorial in the documentation.

Question 12 :

How do I simulate optional parameters to COM calls?

Answer :

You must use the Missing class and pass Missing.Value (in System.Reflection) for any values that have optional parameters.

Question 13 :

What do you know about .NET assemblies?

Answer :

Assemblies are the smallest units of versioning and deployment in the .NET application. Assemblies are also the building blocks for programs such as Web services, Windows services, serviced components, and .NET remoting applications.

Question 14 :

What's the difference between private and shared assembly?

Answer :

Private assembly is used inside an application only and does not have to be identified by a strong name. Shared assembly can be used by multiple applications and has to have a strong name.

Question 15 :

What's a strong name? 

Answer :

A strong name includes the name of the assembly, version number, culture identity, and a public key token.

Question 16 :

How can you tell the application to look for assemblies at the locations other than its own install?

Answer :

Use the directive in the XML .config file for a given application.
< probing privatePath=c:\mylibs; bin\debug />
should do the trick. Or you can add additional search paths in the Properties box of the deployed application.

Question 17 :

How can you debug failed assembly binds? 

Answer :

Use the Assembly Binding Log Viewer (fuslogvw.exe) to find out the paths searched.

Question 18 :

Where are shared assemblies stored?

Answer :

Global assembly cache.

Question 19 :

How can you create a strong name for a .NET assembly? 

Answer :

With the help of Strong Name tool (sn.exe).

Question 20 :

Where's global assembly cache located on the system? 

Answer :

Usually C:\winnt\assembly or C:\windows\assembly.

Question 21 :

What are the ways to deploy an assembly?

Answer :

An MSI installer, a CAB archive, and XCOPY command.

Question 22 :

What is a satellite assembly?

Answer :

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.

Question 23 :

What namespaces are necessary to create a localized application?

Answer :

System.Globalization and System.Resources.

Question 24 :

What is the smallest unit of execution in .NET?

Answer :

an Assembly.

Question 25 :

When should you call the garbage collector in .NET?

Answer :

As a good rule, you should not call the garbage collector. However, you could call the garbage collector when you are done using a large object (or set of objects) to force the garbage collector to dispose of those very large objects from memory. However, this is usually not a good practice.