11. What is the difference between a struct and a class in C#? -
From language spec: The list of similarities between classes and structs is as follows. Longstructs can implement interfaces and can have the same kinds of members as classes. Structs differ from classes in several important ways; however, structs are value types rather than reference types, and inheritance is not supported for structs. Struct values are stored on the stack or in-line. Careful programmers can sometimes enhance performance through judicious use of structs. For example, the use of a struct rather than a class for a Point can make a large difference in the number of memory allocations performed at runtime. The program below creates and initializes an array of 100 points. With Point implemented as a class, 101 separate objects are instantiated-one for the array and one each for the 100 elements.
12. My switch statement works differently than in C++! Why? -
C# does not support an explicit fall through for case blocks. The following code is not legal and will not compile in C#:
switch(x)
{
case 0: // do something
case 1: // do something as continuation of case 0
default: // do something in common with
//0, 1 and everything else
break;
}
To achieve the same effect in C#, the code must be modified as shown below (notice how the control flows are explicit):
class Test
{
public static void Main() {
int x = 3;
switch(x)
{
case 0: // do something
goto case 1;
case 1: // do something in common with 0
goto default;
default: // do something in common with 0, 1, and anything else
break;
}
}
}
13. Is there regular expression (regex) support available to C# developers? -
Yes. The .NET class libraries provide support for regular expressions. Look at the System.Text.RegularExpressions namespace.
14. Is there any sample C# code for simple threading? -
Yes:
using System;
using System.Threading;
class ThreadTest
{
public void runme()
{
Console.WriteLine(”Runme Called”);
}
public static void Main(String[] args)
{
ThreadTest b = new ThreadTest();
Thread t = new Thread(new ThreadStart(b.runme));
t.Start();
}
}
15. Is there an equivalent of exit() for quitting a C# .NET application? -
Yes, you can use System.Environment.Exit(int exitCode) to exit the application or Application.Exit() if it’s a Windows Forms app.
16. 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().
17. 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.”);
}
}
18. Is it possible to restrict the scope of a field/method of a class to the classes in the same namespace? -
There is no way to restrict to a namespace. Namespaces are never units of protection. But if you’re using assemblies, you can use the ‘internal’ access modifier to restrict access to only within the assembly.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment