Posts

Showing posts with the label csharp

C# Interview Questions and Answers Part 3

Image
Will finally block get executed if the exception had not occurred? Yes. What is the C# equivalent of C++ catch (…), which was a catch-all statement for any possible exception? Does C# support try-catch-finally blocks? Yes. Try-catch-finally blocks are supported by the C# compiler. Here's an example of a try-catch-finally block: using System; public class TryTest { static void Main() { try { Console.WriteLine("In Try block"); throw new ArgumentException(); } catch(ArgumentException n1) { Console.WriteLine("Catch Block"); } finally { Console.WriteLine("Finally Block"); } } } Output: In Try Block Catch Block Finally Block If I return out of a try/finally in C#, does the code in the finally-clause run? Yes. The code in the finally always runs. If you return out of the try block, or even if you do a "goto" out of the try, the finally block always runs, as shown in the following example: using System; class main { public static void Main() { t...

C# Interview Questions and Answers Part 13

Image
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 What are the two kinds of properties. Two types of properties in .Net: Get and Set Explain constructor. Constructor is a method in the class which has the same name as the class (in VB.Net its New()). It initializes the member attributes whenever an instance of the class is created. Describe ways of cleaning up objects. Answer1 There is a perfect tool provide by .net frameworks calls Garbage collector, where by mean of GC we can clean up the object and reclaim the memory. The namespace used is System.GC Answer2 the run time will maintain a service called as garbage collector. This service will take care of deallocating memory corresponding to objects....

C# Interview Questions and Answers Part 12

Image
What namespaces are necessary to create a localized application? System.Globalization and System.Resources. What is the smallest unit of execution in .NET? an Assembly. When should you call the garbage collector in .NET? As a good rule, you should not call the garbage collector. However, you could call the garbage collector when you are done using a large object (or set of objects) to force the garbage collector to dispose of those very large objects from memory. However, this is usually not a good practice. How do you convert a value-type to a reference-type? Use Boxing. What happens in memory when you Box and Unbox a value-type? Boxing converts a value-type to a reference-type, thus storing the object on the heap. Unboxing converts a reference-type to a value-type, thus storing the value on the stack.Difference between directcast and ctype. Answer1 DirectCast requires the run-time type of an object variable to bethe same as the specified type.The run-time performance ofDirectCas...

C# Interview Questions and Answers Part 11

Image
What is the difference between a Struct and a Class? Struts are value-type variables and are thus saved on the stack, additional overhead but faster retrieval. Another difference is that struts cannot inherit. What’s the implicit name of the parameter that gets passed into the set method/property of a class? Value. The data type of the value parameter is defined by whatever data type the property is declared as. What does the keyword “virtual” declare for a method or property? The method or property can be overridden. How is method overriding different from method overloading? When overriding a method, you change the behavior of the method for the derived class. Overloading a method simply involves having another method with the same name within the class. Can you declare an override method to be static if the original method is not static? No. The signature of the virtual method must remain the same. (Note: Only the keyword virtual is changed to keyword override) What are the dif...

C# Interview Questions and Answers Part 10

Image
How can you sort the elements of the array in descending order? By calling Sort() and then Reverse() methods. What’s the .NET collection class that allows an element to be accessed using a unique key? HashTable. What class is underneath the SortedList class? A sorted HashTable. Will the finally block get executed if an exception has not occurred? Yes. What’s the C# syntax to catch any possible exception? A catch block that catches the exception of type System.Exception. You can also omit the parameter data type in this case and just write catch {}. Can multiple catch blocks be executed for a single try statement? No. Once the proper catch block processed, control is transferred to the finally block (if there are any). Explain the three services model commonly know as a three-tier application. Presentation (UI), Business (logic and underlying code) and Data (from storage or other sources). What is the syntax to inherit from a class in C#? Place a colon and then the name of the base...

C# Interview Questions and Answers Part 9

Image
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); } } How can you sort the elements of the array in descending order? By calling Sort() and then Reverse() methods. How do you debug an ASP.NET Web application? Attach the aspnet_wp.exe process to the DbgClr debugger. How do you mark a method obsolete? Assuming you've done a "using System;": [Obsolete] public int Foo() {...} or [Obsolete("This is a message describing why this me...

C# Interview Questions and Answers Part 8

Image
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 ...

C# Interview Questions and Answers Part 7

Image
Does C# support C type macros? No. C# does not have macros. Keep in mind that what some of the predefined C macros (for example, __LINE__ and __FILE__) give you can also be found in .NET classes like System.Diagnostics (for example, StackTrace and StackFrame), but they'll only work on debug builds. Can you store multiple data types in System.Array? No. Is it possible to inline assembly or IL in C# code? No. Can you declare the override method static while the original method is non-static? No, you cannot, the signature of the virtual method must remain the same, only the keyword virtual is changed to keyword override Does C# support multiple inheritance? No, use interfaces instead. Can multiple catch blocks be executed? No, once the proper catch code fires off, the control is transferred to the finally block (if there are any), and then whatever follows the finally block. Can you override private virtual methods? No, moreover, you cannot access private methods in inherited cla...

C# Interview Questions and Answers Part 6

Image
What are advantages and disadvantages of Microsoft-provided data provider classes in ADO.NET? SQLServer.NET data provider is high-speed and robust, but requires SQL Server license purchased from Microsoft. OLE-DB.NET is universal for accessing other sources, like Oracle, DB2, Microsoft Access and Informix, but it is a .NET layer on top of OLE layer, so not the fastest thing in the world. ODBC.NET is a deprecated layer provided for backward compatibility to ODBC engines. Why do I get a security exception when I try to run my C# app? 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 ch...

C# Interview Questions and Answers Part 5

Image
How can I create a process that is running a supplied native executable (e.g., cmd.exe)? 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. What is the difference between the System.Array.CopyTo() and System.Array.Clone()? The first one performs a deep copy of the array, the second one is shallow. How do I declare inout arguments in C#? The equivalent of inout in C# is ref. , as shown in the following 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 nee...

C# Interview Questions and Answers Part 4

Image
How do I create a multi language, multi file assembly? Unfortunately, this is currently not supported in the IDE. To do this from the command line, you must compile your projects into netmodules (/target:module on the C# compiler), and then use the command line tool al.exe (alink) to link these netmodules together. C# provides a default constructor for me. I write a constructor that takes a string as a parameter, but want to keep the no parameter one. How many constructors should I write? Two. Once you write at least one constructor, C# cancels the freebie constructor, and now you have to write one yourself, even if there is no implementation in What is the equivalent to regsvr32 and regsvr32 /u a file in .NET development? Try using RegAsm.exe. The general syntax would be: RegAsm. A good description of RegAsm and its associated switches is located in the .NET SDK docs. Just search on "Assembly Registration Tool".Explain ACID rule of thumb for transactions. Transaction mu...

C# Interview Questions and Answers Part 2

Image
How do I simulate optional parameters to COM calls? You must use the Missing class and pass Missing.Value (in System.Reflection) for any values that have optional parameters. What do you know about .NET assemblies? Assemblies are the smallest units of versioning and deployment in the .NET application. Assemblies are also the building blocks for programs such as Web services, Windows services, serviced components, and .NET remoting applications. What’s the difference between private and shared assembly? Private assembly is used inside an application only and does not have to be identified by a strong name. Shared assembly can be used by multiple applications and has to have a strong name. What’s a strong name? A strong name includes the name of the assembly, version number, culture identity, and a public key token. How can you tell the application to look for assemblies at the locations other than its own install? Use the directive in the XML .config file for a given application. ...

C# Interview Questions and Answers Part 1

Image
What's C# ? C# (pronounced C-sharp) is a new object oriented language from Microsoft and is derived from C and C++. It also borrows a lot of concepts from Java too including garbage collection. More at http://msdn.microsoft.com/vstudio/nextgen/technology/csharpintro.asp, http://msdn.microsoft.com/library/default.asp?URL=/library/dotnet/csspec/vclrfcsharpspec_Start.htm and http://msdn.microsoft.com/vstudio/nextgen/technology/csharpdownload.asp Is it possible to inline assembly or IL in C# code? - No. Is it possible to have different access modifiers on the get/set methods of a property? - No. The access modifier on a property applies to both its get and set accessors. What you need to do if you want them to be different is make the property read-only (by only providing a get accessor) and create a private/internal set method that is separate from the property. Is it possible to have a static indexer in C#? allowed in C#. - No. Static indexers are not If I return out of a try/fi...