C#.NET Case Scenarios, Interview Questions and Answers

9/22/2009 No Comment
C#.NET Scenarios ,Job Interview Questions and Answers gathered from various sources

Q: What is the syntax for calling an overloaded constructor within a constructor (this() and constructorname() does not compile)?
A: The syntax for calling another constructor is:
class B
{
B(int i)
{ }
}

class C : B
{
C() : base(5) // call base constructor B(5)
{ }

C(int i) : this() // call C()
{ }

public static void Main() {}
}


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

Q: How do I use enum's in C#?
A: Here's an example:
namespace Foo
{
enum Colors
{
BLUE,
GREEN
}

class Bar
{
Colors color;
Bar() { color = Colors.GREEN;}

public static void Main() {}
}
}

Q: Why do I get an error (CS1006) when trying to declare a method without specifying a return type?
A: If you leave off the return type on a method declaration the compiler thinks you are trying to declare a constructor.

So if you are trying to declare a method that returns nothing, use void.

For example: Here's an example:
// This results in a CS1006 error
public static staticMethod (mainStatic obj)

// This will work as wanted
public static void staticMethod (mainStatic obj)

Q: I have several source files, each has a Main() method. How do I specify which of those Main()'s is supposed to be used?
A: The entry point of your program must be static, named Main, have arguments of either none or string[], and return either int or void.

The C# compiler now allows you to have multiple Main() methods in your application, but requires you to specify the fully qualified class name that has the Main() method you want to use. You specify the class by using the /main compiler option. (e.g. csc /main:MyClass *.cs)

Note that the way to set it in the Visual Studio .NET IDE is through -> Project Properties, Common properties, General, and then setting the Startup object to the name of the class that contains the main method you want to use.

Q: Does Console.WriteLine() stop printing when it reaches a NULL character within a string?
A: String 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.

Q: Is it possible to use multicast delegates in C#? What's the syntax?
A: All delegates in C# are multicast, therefore there is no 'multicast' keyword as there was in Visual J++.

Q: How do I create a Delegate/MulticastDelegate?
A: C# simply requires 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!

A: Make sure that the target type set in the project properties setting is set to Windows Application, and not Console Application. If youre using the command line, compile with /target:winexe & not /target:exe.

Q: Is there a way to force garbage collection?
A: 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().

Q: Does C# support C type macros?
A: No. C# does not have macros.

Keep in mind that what some of the predefined C macros (e.g. __LINE__ and __FILE__) give you can also be found in COM+ classes like System.Diagnostics (e.g. StackTrace and StackFrame), but it'll only work on debug builds.

Q: Why is the compiler referencing things that I'm not telling it to reference?
A: The C# compiler automatically references all the assemblies listed in the 'csc.rsp' file. You can disable the usage of the csc.rsp file by using the /noconfig compiler option on your command line.
Note that the Visual Studio .NET IDE never uses the response file.

Q: How do you directly call a native function exported from a DLL?
A: 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 which is implemented in a native DLL. The method C.MessageBoxA() is declared with the static and extern 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.

Q: I'm trying to implement an interface defined in COM+ runtime. "public Object* GetObject() { ... }" doesn't seem to work. What can I do?
A: In managed C++ the "Object* GetObject()" (pointer to Object) syntax is required. But in C#, it should work by just using "public Object GetObject() { ... }" instead.

Q: Does C# support templates?
A: 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 runtime as opposed to compile time. You can read more about them here.

Q: Why do I get a CS0117 error when attempting to use the 'Item' property?
A: Properties are supported in C#, but the "Item" property is special on classes - it is actually the default indexed property. In C# the way to access these is to simply leave the "Item" specifier off.

Here's a simple program:
using System;
using System.Collections;

class Test
{
public static void Main()
{
ArrayList al = new ArrayList();
al.Add( new Test() );
al.Add( new Test() );
Console.WriteLine("First Element is {0}", al[0]);
}
}
Notice the way that you don't use ".Item[0]" in the WriteLine.

Q: I was trying to use an "out int" parameter in one of my functions. How should I declare the variable which I am passing to it?
A: You should declare the variable as an int, but when you pass it in you must specify it as 'out' like the following:
int i;
foo(out i);
where foo is declared as follows:
[return-type] foo(out int o) { }


Q: Is there an equivalent to C++'s reference parameters in C#? (i.e. void foo(int &i))
A: We call them ref parameters:
class Test
{
public void foo(ref int i)
{
i = 1;
}

public void bar()
{
int a = 0;
foo(ref a);
if (a == 1)
Console.WriteLine("It worked");
}

public static void Main() {}
}
Note that you're required to restate the ref in a method invocation.

Q: How do I declare inout arguments in C#?
A: The equivalent of inout in C# is ref. For example:
public void MyMethod (ref String str1, out String str2)
{
...
}
When calling the method, it would be called like this:
String s1;
String s2;
s1 = "Hello";
MyMethod(ref s1, out s2);
Console.WriteLine(s1);
Console.WriteLine(s2);
Notice that you need to specify ref when declaring the function and calling it.

Q: How do destructors and garbage collection work in C#?
A: 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.

Q: Why do I get a "CS5001: does not have an entry point defined" error when compiling?
A: The most common problem is that you used a lowercase 'm' when defining the Main method. The correct way to implement the entry point is:
class test
{
static void Main(string[] args) {}
}

Q: How do I port "synchronized" functions from Visual J++ to C#?
A: Original Visual J++ code:
public synchronized void Run()
{
// function body
}
Ported C# code:
class C
{
public void Run()
{
lock(this)
{
// function body
}
}

public static void Main() {}
}

Q: How do you implement thread synchronization (Object.Wait, Notify, and CriticalSection) in C#?
A: You want the lock statement which is the same as Monitor Enter/Exit:
lock(obj)
{
// code
}
translates to:
try
{
CriticalSection.Enter(obj);
// code
}
finally
{
CriticalSection.Exit(obj);
}


Q: What's the syntax for a static initializer for my class?
A: The following is a class with a static initializer block:
class MyClass
{
static MyClass()
{
// initialize static variables here
}

public static void Main() {}
}

Q: Is it possible to have different access modifiers on the get/set methods of a property?
A: No. The access modifier on a property applies to both it's get and set accessors. What you probably want to do if you need them to be different is make the property read-only (by only providing a get accessor) and creating a private/internal set method separate from the property.
Q: How do I create a multi-language single-file assembly?
A: This is currently not supported by Visual Studio .NET.

Q: Does C# support properties of array types?
A: 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;
}
}
Related Posts


No comments :

 

Aired | The content is copyrighted and may not be reproduced on other websites. | Copyright © 2009-2016 | All Rights Reserved 2016

Contact Us | About Us | Privacy Policy and Disclaimer