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

1⟩ Explain ACID rule of thumb for transactions.

Transaction must be Atomic (it is one unit of work and does not dependent on previous and following transactions), Consistent (data is either committed or roll back, no in-between case where something has been updated and something hasnot), Isolated (no transaction sees the intermediate results of the current transaction), Durable (the values persist if the data had been committed even if the system crashes right after).

 162 views

9⟩ What optimizations does the C# compiler perform when you use the /optimize+ compiler option?

The following is a response from a developer on the C# compiler team:

We get rid of unused locals (i.e., locals that are never read, even if assigned).

We get rid of unreachable code.

We get rid of try-catch w/ an empty try.

We get rid of try-finally w/ an empty try (convert to normal code...).

We get rid of try-finally w/ an empty finally (convert to normal code...).

We optimize branches over branches:

gotoif A, lab1

goto lab2:

lab1:

turns into: gotoif !A, lab2

lab1:

We optimize branches to ret, branches to next instruction, and branches to branches.

 166 views

10⟩ How can I create a process that is running a supplied native executable (e.g., cmd.exe)?

The following code should run the executable and wait for it to exit before

continuing: using System;

using System.Diagnostics;

public class ProcessTest {

public static void Main(string[] args) {

Process p = Process.Start(args[0]);

p.WaitForExit();

Console.WriteLine(args[0] + " exited.");

}

}

Remember to add a reference to System.Diagnostics.dll when you compile.

 196 views

12⟩ How do I declare inout arguments in C#?

The equivalent of inout in C# is ref. , as shown in the following

example: public void MyMethod (ref String str1, out String str2)

{

...

}

When calling the method, it would be called like this: String s1;

String s2;

s1 = "Hello";

MyMethod(ref s1, out s2);

Console.WriteLine(s1);

Console.WriteLine(s2);

Notice that you need to specify ref when declaring the function and calling it.

 153 views

13⟩ 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:

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).

 158 views

14⟩ What is the difference between const and static read-only?

The difference is that static read-only can be modified by the containing class, but const can never be modified and must be initialized to a compile time constant. To expand on the static read-only case a bit, the containing class can only modify it: -- in the variable declaration (through a variable initializer).

-- in the static constructor (instance constructors if it's not static).

 154 views

20⟩ From a versioning perspective, what are the drawbacks of extending an interface as opposed to extending a class?

With regard to versioning, interfaces are less flexible than classes. With a class, you can ship version 1 and then, in version 2, decide to add another method. As long as the method is not abstract (i.e., as long as you provide a default implementation of the method), any existing derived classes continue to function with no changes. Because interfaces do not support implementation inheritance, this same pattern does not hold for interfaces. Adding a method to an interface is like adding an abstract method to a base class--any class that implements the interface will break, because the class doesn't implement the new interface method.

 167 views