• No results found

What are the features of abstract class?

In document C# Interview Questions &Answer (Page 25-35)

99. What are the features of abstract class?

An abstract class cannot be instantiated, and it is an error to use the new operator on an abstract class.

An abstract class is permitted (but not required) to contain abstract methods and accessors.

An abstract class cannot be scaled.

100. What is the difference between console and window application?

A console application, which is designed to run at the command line with no user interface.

A Windows application, which is designed to run on a user’s desktop and has a user interface.

101. How do I get deterministic finalization in C#?

In a garbage collected environment, it's impossible to get true determinism. However, a design pattern that we recommend is implementing IDisposable on any class that contains a critical resource. Whenever this class is consumed, it may be placed in a using statement, as shown in the following example:

using(FileStream myFile = File.Open(@"c:temptest.txt", FileMode.Open))

{

int fileOffset = 0;

while(fileOffset < myFile.Length) {

Console.Write((char)myFile.ReadByte());

fileOffset++;

} }

When myFile leaves the lexical scope of the using, its dispose method will be called.

102.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').

103. What is difference between the “throw” and “throw ex” in .NET?

“Throw” statement preserves original error stack whereas “throw ex” have the stack trace from their throw point. It is always advised to use “throw” because it provides more accurate error information.

104. 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);

} }

105.How do you directly call a native function exported from a DLL?

Here's a quick example of the Dll Import 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.

106. What is the difference between Finalize() and Dispose() methods?

Dispose() is called when we want for an object to release any unmanaged resources with them. On the other hand Finalize() is used for the same purpose but it doesn’t assure the garbage collection of an object.

107. How do you specify a custom attribute for the entire assembly (rather than for a class)?

Global attributes must appear after any top-level using clauses and before the first type or namespace declarations. An example of this is as follows:

using System;

[assembly : MyAttributeClass]

class X { }

Note that in an IDE-created project, by convention, these attributes are placed in AssemblyInfo.cs.

108. What is the difference between a struct and a class in C#?

From language spec:

The list of similarities between classes and structs is as follows. Longstructs can implement interfaces and can have the same kinds of members as classes. Structs differ from classes in several important ways; however, structs are value types rather than reference types, and inheritance is not supported for structs.

Struct values are stored on the stack or in-line. Careful programmers can sometimes enhance performance through judicious use of structs. For example, the use of a struct rather than a class for a Point can make a large difference in the number of memory allocations performed at runtime.

109. What is the difference between the Debug class and Trace class?

Documentation looks the same. Use Debug class for debug builds, use Trace class for both debug and release builds.

110. How can you overload a method?

Different parameter data types, different number of parameters, different order of parameters.

111. What debugging tools come with the .NET SDK?

CorDBG - command-line debugger, and DbgCLR - graphic debugger. Visual Studio .NET uses the DbgCLR. To use CorDbg, you must compile the original C# file using the /debug switch.

112. What does Dispose method do with the connection object?

Deletes it from the memory.

113. What is an object pool in .NET?

An object pool is a container having objects ready to be used. It tracks the object that is currently in use, total number of objects in the pool. This reduces the overhead of creating and re-creating objects.

114.When you inherit a protected class-level variable, who is it available to?

Classes in the same namespace.

115. How can I get the ASCII code for a character in C#?

Casting the char to an int will give you the ASCII value: char c = 'f';

System.Console.WriteLine((int)c); or for a character in a string:

System.Console.WriteLine((int)s[3]); The base class libraries also offer ways to do this with the Convert class or Encoding classes if you need a particular encoding.

117. 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!

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

119. How can you sort the elements of the array in descending order?

By calling Sort() and then Reverse() methods.

120. How do you debug an ASP.NET Web application?

Attach the aspnet_wp.exe process to the DbgClr debugger.

121. What are Custom Exceptions?

Sometimes there are some errors that need to be handeled as per user requirements.

Custom exceptions are used for them and are used defined exceptions.

122. How do you inherit a class into other class in C#?

Colon is used as inheritance operator in C#. Just place a colon and then the class name.

public class DerivedClass : BaseClass

123. What are the ways to deploy an assembly?

An MSI installer, a CAB archive, and XCOPY command.

124. What is serialization?

When we want to transport an object through network then we have to convert the object into a stream of bytes. The process of converting an object into a stream of bytes is called Serialization.

For an object to be serializable, it should inherit ISerialize Interface.

De-serialization is the reverse process of creating an object from a stream of bytes.

125. What is a delegate?

A delegate object encapsulates a reference to a method. In C++ they were referred to as function pointers.

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

127. What is an abstract class?

A class that cannot be instantiated. A concept in C++ known as pure virtual method. A class that must be inherited and have the methods over-ridden. Essentially, it is a blueprint for a class without any implementation.

128.Does C# support multiple-inheritance?

No.

129. What is the use of using statement in C#?

The using block is used to obtain a resource and use it and then automatically dispose of when the execution of block completed.

130. What is difference between constants and read-only?

Constant variables are declared and initialized at compile time. The value can’t be changed after wards.

Read-only variables will be initialized only from the Static constructor of the class. Read only is used only when we want to assign the value at run time.

131. What’s the top .NET class that everything is derived from?

System.Object.

132. What does the term immutable mean?

The data value may not be changed. Note: The variable value may be changed, but the original immutable data value was discarded and a new data value was created in memory.

133. What’s the difference between System.String and System.Text.StringBuilder classes?

System.String is immutable. System.StringBuilder was designed with the purpose of having a mutable string where a variety of operations can be performed.

134. What’s the advantage of using System.Text.StringBuilder over System.String?

StringBuilder is more efficient in cases where there is a large amount of string manipulation. Strings are immutable, so each time a string is changed, a new instance in memory is created.

135. What’s 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. A shallow copy of an Array copies only the elements of the Array, whether they are reference types or value types, but it does not copy the objects that the references refer to. The references in the new Array point to the same objects that the references in the original Array point to. In contrast, a deep copy of an Array copies the elements and everything directly or indirectly referenced by the elements.

136. How can you sort the elements of the array in descending order?

By calling Sort() and then Reverse() methods.

137. Can multiple catch blocks be executed?

No, Multiple catch blocks can’t be executed. Once the proper catch code executed, the control is transferred to the finally block and then the code that follows the finally block gets executed.

138. What class is underneath the SortedList class?

A sorted HashTable.

139.Will the finally block get executed if an exception has not occurred?

Yes.

140. 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 {}.

141. 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).

142. 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).

143. What is the syntax to inherit from a class in C#?

Place a colon and then the name of the base class.

Example:

class MyNewClass : MyBaseClass

144. Can you prevent your class from being inherited by another class?

Yes. The keyword “sealed” will prevent the class from being inherited.

145. Can you allow a class to be inherited, but prevent the method from being over-ridden?

Yes. Just leave the class public and make the method sealed.

146. What’s an abstract class?

A class that cannot be instantiated. An abstract class is a class that must be inherited and have the methods overridden. An abstract class is essentially a blueprint for a class without any

implementation.

147. What is an interface class?

Interfaces, like classes, define a set of properties, methods, and events. But unlike classes, interfaces do not provide implementation. They are implemented by classes, and defined as separate entities from classes.

148. What’s the difference between an interface and abstract class?

Interfaces have all the methods having only declaration but no definition.

In an abstract class, we can have some concrete methods.

In an interface class, all the methods are public.

An abstract class may have private methods.

149. Why can’t you specify the accessibility modifier for methods inside the interface?

They all must be public, and are therefore public by default.

In document C# Interview Questions &Answer (Page 25-35)

Related documents