Friday, 18 September 2015

Latest C#.Net Interview Questions and Answers

51. What is the syntax for calling an overloaded constructor within a constructor (this() and constructorname() does not compile)? 
The syntax for calling another constructor is as follows:
class B
B(int i)
{ }
class C : B
C() : base(5) // call base constructor B(5)
{ }
C(int i) : this() // call C()
{ }
public static void Main() {}

52. Why do I get a "CS5001: does not have an entry point defined" error when compiling? 
The most common problem is that you used a lowercase 'm' when defining the Main method. The correct way to implement the entry point is as follows:
class test
static void Main(string[] args) {}

53. What does the keyword virtual mean in the method definition? 
The method can be over-ridden.
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:
turns into: gotoif !A, lab2
We optimize branches to ret, branches to next instruction, and branches to branches.

54. 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]);
Console.WriteLine(args[0] + " exited.");
Remember to add a reference to System.Diagnostics.dll when you compile.

55. What is the difference between the System.Array.CopyTo() and System.Array.Clone()? 
The first one performs a deep copy of the array, the second one is shallow.

56. 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);
Notice that you need to specify ref when declaring the function and calling it.

57. Is there a way of specifying which block or loop to break out of when working with nested loops? 
The easiest way is to use goto: using System;
class BreakExample
public static void Main(String[] args)
for(int i=0; i<3; i++)
Console.WriteLine("Pass {0}: ", i);
for( int j=0 ; j<100 ; j++ )
if ( j == 10) goto done;
Console.WriteLine("{0} ", j);
Console.WriteLine("This will not print");
Console.WriteLine("Loops complete.");

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

59. What does the parameter Initial Catalog define inside Connection String?
The database name to connect to.

60. What is the difference between System.String and System.StringBuilder classes? 
System.String is immutable; System.StringBuilder was designed with the purpose of having a mutable string where a variety of operations can be performed.

More Questions & Answers:-

No comments:

Post a Comment