Adv C#
Programming


C# Interview Questions and Answers Part 6
51.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) {}
}
52. What are the different ways a method can be overloaded?
-
Methods can be overloaded using different data types for parameter, different order of parameters, and different number of parameters.
53. Why can’t you specify the accessibility modifier for methods inside the interface?
-
In an interface, we have virtual methods that do not have method definition. All the methods are there to be overridden in the derived class. That’s why they all are public.
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)
{
string app;
app = Console.ReadLine();
Process p = Process.Start(app+".exe");
p.WaitForExit();
Console.WriteLine(app+".exe" + " exited.");
}
}
-
Remember to add a reference to System.Diagnostics.dll when you compile.
55. List down the commonly used types of exceptions in .Net?
-
ArgumentException, ArgumentNullException , ArgumentOutOfRangeException,
-
ArithmeticException, DivideByZeroException ,OverflowException ,
-
IndexOutOfRangeException ,InvalidCastException
-
,InvalidOperationException , IOEndOfStreamException ,
-
NullReferenceException , OutOfMemoryException , StackOverflowException etc.
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);
Console.WriteLine(s1);
Console.WriteLine(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");
}
done:
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 Array and Arraylist?
-
In an array, we can have items of the same type only. The size of the array is fixed.
-
An arraylist is similar to an array but it doesn’t have a fixed size.