C# (Sharp) Programming Language

  Home  Microsoft .Net Technologies  C# (Sharp) Programming Language


“Learn C# (Sharp) Programming Language by Interview Questions and Answers”



163 C# (Sharp) Programming Language Questions And Answers

144⟩ What does assert() do in C#?

In debug compilation, assert takes in a Boolean condition as a parameter, and shows the error dialog if the condition is false. The program proceeds without any interruption if the condition is true.

 204 views

145⟩ 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').

 199 views

146⟩ 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.

 200 views

157⟩ Difference between directcast and ctype.

Anser1

DirectCast requires the run-time type of an object variable to bethe same as the specified type.The run-time performance ofDirectCast is better than that of CType, if the specified type and the run-time typeof the expression are the same. Ctype works fine if there is a valid conversion defined between the expression and the type.

Answer2

The difference between the two keywords is that CType succeeds as long as there is a valid conversion defined between the expression and the type, whereas DirectCast requires the run-time type of an object variable to be the same as the specified type. If the specified type and the run-time type of the expression are the same, however, the run-time performance of DirectCast is better than that of CType.

 223 views

158⟩ An example of a ctype and directcast.

In the preceding example, the run-time type of Q is Double. CType succeeds because Double can be converted to Integer, but DirectCast fails because the run-time type of Q is not already Integer

 194 views

159⟩ Explain manifest & metadata in C#.

Answer1

Manifest is metadata about assemblies. Metadata is machine-readable information about a resource, or “”data about data.” In .NET, metadata includes type definitions, version information, external assembly references, and other standardized information.

Answer2

Manifest: Manifest describes assembly itself. Assembly Name, version number, culture, strong name, list of all files, Type references, and referenced assemblies.

Metadata: Metadata describes contents in an assembly classes, interfaces, enums, structs, etc., and their containing namespaces, the name of each type, its visibility/scope, its base class, the nterfaces it implemented, its methods and their scope, and each method’s parameters, type’s properties, and so on.

 220 views

160⟩ Difference between value and reference type.

what are value types and reference types?

Value type - bool, byte, chat, decimal, double, enum , float, int, long, sbyte, short, strut, uint, ulong, ushort

Value types are stored in the Stack

Reference type - class, delegate, interface, object, string

Reference types are stored in the Heap

 223 views