Thursday, January 3, 2008
101 How do destructors and garbage collection work in C#?
C# has finalizers (similar to destructors except that the runtime doesn't guarantee they'll be
called), and they are specified as follows:
class C
{
~C()
{
// your code
}
public static void Main() {}
}
Currently, they override object.Finalize(), which is called during the GC process.
102 How can I get the ASCII code for a character in C#?
Casting the char to an int will give you the ASCII value: char c = 'f';
System.Console.WriteLine((int)c);
or for a character in a string: System.Console.WriteLine((int)s[3]);
The base class libraries also offer ways to do this with the Convert class or Encoding classes if
you need a particular encoding.
103 How can I get around scope problems in a try/catch?
If you try to instantiate the class inside the try, it'll be out of scope when you try to access it from
the catch block. A way to get around this is to do the following:
Connection conn = null;
try
{
conn = new Connection();
conn.Open();
}
finally
{
if (conn != null) conn.Close();
}
By setting it to null before the try block, you avoid getting the CS0165 error (Use of possibly
unassigned local variable 'conn').
104 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.
105 How can I access the registry from C# code?
By using the Registry and RegistryKey classes in Microsoft.Win32, you can easily access the
registry. The following is a sample that reads a key and displays its value: using System;using
Microsoft.Win32;
class regTest
{
public static void Main(String[] args)
{
RegistryKey regKey;
Object value;
regKey = Registry.LocalMachine;
regKey =
regKey.OpenSubKey("HARDWARE\DESCRIPTION\System\CentralProcessor\0");
value = regKey.GetValue("VendorIdentifier");
Console.WriteLine("The central processor of this machine is: {0}.", value);
}
}
106 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.
107 Does Console.WriteLine() stop printing when it reaches a NULL character within a string?
Strings are not null terminated in the runtime, so embedded nulls are allowed.
Console.WriteLine() and all similar methods continue until the end of the string.
108 Does C# support try-catch-finally blocks?
Yes. Try-catch-finally blocks are supported by the C# compiler.
Here's an example of a try-catch-finally block: using System;
public class TryTest
{
static void Main()
{
try
{
Console.WriteLine("In Try block");
throw new ArgumentException();
}
catch(ArgumentException n1)
{
Console.WriteLine("Catch Block");
}
finally
{
Console.WriteLine("Finally Block");
}
}
}
Output: In Try Block
Catch Block
Finally Block
109 Does C# support templates?
No. However, there are plans for C# to support a type of template known as a generic. These
generic types have similar syntax but are instantiated at run time as opposed to compile time.
You can read more about them here.
110 Does C# support properties of array types?
Yes. Here's a simple example: using System;
class Class1
{
private string[] MyField;
public string[] MyProperty
{
get { return MyField; }
set { MyField = value; }
}
}
class MainClass
{
public static int Main(string[] args)
{
Class1 c = new Class1();
string[] arr = new string[] {"apple", "banana"};
c.MyProperty = arr;
Console.WriteLine(c.MyProperty[0]); // "apple"
return 0;
}
}
111 Does C# support parameterized properties?
No. C# does, however, support the concept of an indexer from language spec.
An indexer is a member that enables an object to be indexed in the same way as an array.
Whereas properties enable field-like access, indexers enable array-like access.
As an example, consider the Stack class presented earlier. The designer of this class may want
to expose array-like access so that it is possible to inspect or alter the items on the stack without performing unnecessary Push and Pop operations. That is, Stack is implemented as a linked list,
but it also provides the convenience of array access.
Indexer declarations are similar to property declarations, with the main differences being that
indexers are nameless (the name used in the declaration is this, since this is being indexed) and
that indexers include indexing parameters. The indexing parameters are provided between
square brackets.
112 Does C# support C type macros?
No. C# does not have macros. Keep in mind that what some of the predefined C macros (for
example, __LINE__ and __FILE__) give you can also be found in .NET classes like
System.Diagnostics (for example, StackTrace and StackFrame), but they'll only work on debug
builds.
113 Does C# support #define for defining global constants?
No. If you want to get something that works like the following C code:
#define A 1
use the following C# code: class MyConstants
{
public const int A = 1;
}
Then you use MyConstants.A where you would otherwise use the A macro.
Using MyConstants.A has the same generated code as using the literal 1.
114 Can I define a type that is an alias of another type (like typedef in C++)?
Not exactly. You can create an alias within a single file with the "using" directive: using System;
using Integer = System.Int32; // alias
But you can't create a true alias, one that extends beyond the file in which it is declared. Refer to the C# spec for more info on the 'using' statement's scope.
115 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).
Labels: interview questions 6
0 comments:
Post a Comment