October 20, 2018

Srikaanth

Apigee Most Frequently Asked Latest C# Interview Questions Answers

How Do I Declare A Pure Virtual Function In C#?

Use the abstract modifier on the method. The class must also be marked as abstract (naturally). Note that abstract methods cannot have an implementation (unlike pure virtual C++ methods).

Can I Call A Virtual Method From A Constructor/destructor?

Yes, but it's generally not a good idea. The mechanics of object construction in .NET are quite different from C++, and this affects virtual method calls in constructors.

C++ constructs objects from base to derived, so when the base constructor is executing the object is effectively a base object, and virtual method calls are routed to the base class implementation. By contrast, in .NET the derived constructor is executed first, which means the object is always a derived object and virtual method calls are always routed to the derived implementation. (Note that the C# compiler inserts a call to the base class constructor at the start of the derived constructor, thus preserving standard OO semantics by creating the illusion that the base constructor is executed first.)

The same issue arises when calling virtual methods from C# destructors. A virtual method call in a base destructor will be routed to the derived implementation.

Should I Make My Destructor Virtual?

A C# destructor is really just an override of the System.Object Finalize method, and so is virtual by definition.

Can I Use Exceptions In C#?

Yes, in fact exceptions are the recommended error-handling mechanism in C# (and in .NET in general). Most of the .NET framework classes use exceptions to signal errors.

What Types Of Object Can I Throw As Exceptions?

Only instances of the System.Exception classes, or classes derived from System.Exception. This is in sharp contrast with C++ where instances of almost any type can be thrown.

Can I Define My Own Exceptions?

Yes, just derive your exception class from System.Exception.
Apigee Most Frequently Asked Latest C# Interview Questions Answers
Apigee Most Frequently Asked Latest C# Interview Questions Answers

Does The System.exception Class Have Any Cool Features?

Yes - the feature which stands out is the StackTrace property. This provides a call stack which records where the exception was thrown from. For example, the following code
using System;
class CApp
{
public static void Main()
{
try
{
f();
}
catch( Exception e )
{
Console.WriteLine( "System.Exception stack trace = \n{0}", e.StackTrace );
}
}
static void f()
{
throw new Exception( "f went pear-shaped" );
}
}
produces this output
System.Exception stack trace =
at CApp.f()
at CApp.Main()

Note, however, that this stack trace was produced from a debug build. A release build may optimise away some of the method calls which could mean that the call stack isn't quite what you expect.


Does C# Support A Variable Number Of Arguments?

Yes, using the params keyword. The arguments are specified as a list of arguments of a specific type, e.g. int. For ultimate flexibility, the type can be object. The standard example of a method which uses this approach is System.Console.WriteLine().

Is There An Equivalent To The Instanceof Operator In Visual J++?

C# has the is operator
expr is type

How Do I Create A Delegate/multicastdelegate?

C# requires only a single parameter for delegates: the method address. Unlike other languages, where the programmer must specify an object reference and the method to invoke, C# can infer both pieces of information by just specifying the method's name. For example, let's use System.Threading.ThreadStart: Foo MyFoo = new Foo(); ThreadStart del = new ThreadStart(MyFoo.Baz); This means that delegates can invoke static class methods and instance methods with the exact same syntax!

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.

My Switch Statement Works Differently! 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 in common with 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;
}
}
}

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("HARDWAREDESCRIPTIONSystemCentralProcessor ");
value = regKey.GetValue("VendorIdentifier");
Console.WriteLine("The central processor of this machine is: {0}.", value);
}
}

Why Does Dllimport Not Work For Me?

All methods marked with the DllImport attribute must be marked as public static extern.

What Is The Difference Between An Interface And Abstract Class?

In the interface all methods must be abstract; in the abstract class some methods can be concrete. In the interface no accessibility modifiers are allowed, which is ok in abstract classes.

Ctype(123.34,integer) - Should It Throw An Error? Why Or Why Not?

It would work fine. As the runtime type of 123.34 would be double, and Double can be converted to Integer.

the ctype(123.34,integer) will work fine no errors.

Subscribe to get more Posts :