C# Programs, Code Examples,Interview Question and Solutions

9/07/2009 No Comment

Microsoft C#.NET Interview Questions and Answers, C#.NET Programs, C# Code Examples,C# Interview Solutions.

Q: What optimizations does the C# compiler perform when you use /optimize+ compiler option?A: Here's the response from a developer on the C# compiler team:

We get rid of unused locals (ie, locals which are never read, even if assigned).
We get rid of unreachable code.
We get rid of try-catch w/ an empty try.
We get rid of try-finally w/ an empty try (convert to normal code...).
We get rid of try-finally w/ an empty finally (convert to normal code...).
We optimize branches over branches:
gotoif A, lab1
goto lab2:
lab1:
turns into:
gotoif !A, lab2
lab1:
We optimize branches to ret, branches to next instruction, branches to branches.

Q: How can I access the registry from C# code?

A: 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 it's 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);
}
}
Q: Does C# support #define for defining global constants?
A: No. If you want to get something that works like the following C code: #define A 1 can be 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.

Q: How can I create a process that is running a supplied native executable (e.g. cmd.exe)?
A: 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.

Q: How do you mark a method obsolete?
A: Assuming you've done a "using System;": [Obsolete] public int Foo() {...} or [Obsolete("This is a message describing why this method is obsolete")] public int Foo() {...} Note: The O in Obsolete is capitalized.

Q: I've added a using to my C# source file, but its still telling me that I have undefined types. What am I doing wrong?
A: It's likely that youre missing the reference to that assembly in which the namespace is located. Using is simply a syntax convenience, in order to add a dependency to another assembly, you must reference it in the IDE you right click your project and select add reference, from the command line you use the /reference switch.

Is there any sample C# code for simple threading?
A: Here's some:
using System;
using System.Threading;
class ThreadTest
{
public void runme()
{
Console.WriteLine("Runme Called");
}

public static void Main(String[] args)
{
ThreadTest b = new ThreadTest();
Thread t = new Thread(new ThreadStart(b.runme));
t.Start();
}
}
Q: How do I call the base class' implementation of an overrided method?A: You can call it using 'base.methodname()'. For example:
public class MyBase
{
public virtual void meth()
{
System.Console.WriteLine("Test");
}
}

public class MyDerived : MyBase
{
public override void meth()
{
System.Console.WriteLine("Test2");
base.meth();
}

public static void Main()
{
MyDerived md = new MyDerived();
md.meth();
}
}

Q: Is there regular expression (regex) support available to C# developers?
A: Yes. The .NET class libraries provide support for regular expressions. Look at the documentation for the System.Text.RegularExpressions namespace.

Q: Why do I get a security exception when I try to run my C# app?
A: Some security exceptions are thrown if you are working on a network share. There are some parts of the frameworks that will not run if being run off a share (roaming profile, mapped drives, etc.). To see if this is what's happening, just move the executable over to your local drive and see if it runs without the exceptions. One of the common exceptions thrown under these conditions is System.Security.SecurityException. To get around this, you can change your security policy for the intranet zone, code group 1.2, (the zone that running off shared folders falls into) using the caspol.exe tool.

Q: How can I get around scope problems in a try/catch?A: 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').

Q: What is the equivalent to regsvr32 and regsvr32 /u a file in .NET development?
A: Try using RegAsm.exe. The general syntax would be: RegAsm there's a pretty good description of it and it's associated switches in the .NET SDK docs. Just search on "Assembly Registration Tool".
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