Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Wednesday, July 16, 2008

C# Interview Questions and Answers 10

How do I convert a string to an int in C#?
Here's an example: using System;
class StringToInt
{
public static void Main()
{
String s = "105";
int x = Convert.ToInt32(s);
Console.WriteLine(x);
}
}

How do you directly call a native function exported from a DLL?
Here's a quick example of the DllImport attribute in action: using System.Runtime.InteropServices;
class C
{
[DllImport("user32.dll")]
public static extern int MessageBoxA(int h, string m, string c, int type);
public static int Main()
{
return MessageBoxA(0, "Hello World!", "Caption", 0);
}
}
This example shows the minimum requirements for declaring a C# method that is implemented in a native DLL. The method C.MessageBoxA() is declared with the static and external modifiers, and has the DllImport attribute, which tells the compiler that the implementation comes from the user32.dll, using the default name of MessageBoxA. For more information, look at the Platform Invoke tutorial in the documentation.

What is the .NET datatype that allows the retrieval of data by a unique key?
HashTable.

How do you specify a custom attribute for the entire assembly (rather than for a class)?
Global attributes must appear after any top-level using clauses and before the first type or namespace declarations. An example of this is as follows:
using System;
[assembly : MyAttributeClass]
class X {}
Note that in an IDE-created project, by convention, these attributes are placed in
AssemblyInfo.cs.

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.

What is the difference between the Debug class and Trace class?
Documentation looks the same. Use Debug class for debug builds, use Trace class for both debug and release builds.

How can you overload a method?
Different parameter data types, different number of parameters, different order of parameters.

What debugging tools come with the .NET SDK?
CorDBG - command-line debugger, and DbgCLR - graphic debugger. Visual Studio .NET uses the DbgCLR. To use CorDbg, you must compile the original C# file using the /debug switch.

What does Dispose method do with the connection object?
Deletes it from the memory.

How do you generate documentation from the C# file commented properly with a command-line compiler?
Compile it with a /doc switch.

When you inherit a protected class-level variable, who is it available to?
Classes in the same namespace.

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.

Is there an equivalent to the instanceof operator in Visual J++?
C# has the is operator:
expr is type

No comments:

Archives