Set - 6

Question 1 :

Can I define a type that is an alias of another type (like typedef in C++)?

Answer :

Not exactly. You can create an alias within a single file with the "using" directive:

using System; using Integer = System.Int32; // alias

But you can't create a true alias, one that extends beyond the file in which it is declared. Refer to the C# spec for more info on the 'using' statement's scope.


Question 2 :

Is it possible to have different access modifiers on the get/set methods of a property?

Answer :

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.


Question 3 :

Is it possible to have a static indexer in C#?

Answer :

No. Static indexers are not allowed in C#.


Question 4 :

Does C# support #define for defining global constants?

Answer :

No. If you want to get something that works like the following C code:

#define A 1
use 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.


Question 5 :

Does C# support templates?

Answer :

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 run time as opposed to compile time. You can read more about them here.


Question 6 :

Does C# support parameterized properties?

Answer :

No. C# does, however, support the concept of an indexer from language spec. An indexer is a member that enables an object to be indexed in the same way as an array. Whereas properties enable field-like access, indexers enable array-like access. As an example, consider the Stack class presented earlier. The designer of this class may want to expose array-like access so that it is possible to inspect or alter the items on the stack without performing unnecessary Push and Pop operations. That is, Stack is implemented as a linked list, but it also provides the convenience of array access.
Indexer declarations are similar to property declarations, with the main differences being that indexers are nameless (the name used in the declaration is this, since this is being indexed) and that indexers include indexing parameters. The indexing parameters are provided between square brackets.


Question 7 :

Does C# support C type macros?

Answer :

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.


Question 8 :

Can you store multiple data types in System.Array?

Answer :

No.


Question 9 :

Is it possible to inline assembly or IL in C# code?

Answer :

No.


Question 10 :

Can you declare the override method static while the original method is non-static?

Answer :

No, you cannot, the signature of the virtual method must remain the same, only the keyword virtual is changed to keyword override


Question 11 :

Does C# support multiple inheritance?

Answer :

No, use interfaces instead.


Question 12 :

Can multiple catch blocks be executed?

Answer :

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.


Question 13 :

Can you override private virtual methods?

Answer :

No, moreover, you cannot access private methods in inherited classes, have to be protected in the base class to allow any sort of access.


Question 14 :

What is a pre-requisite for connection pooling?

Answer :

Multiple processes must agree that they will share the same connection, where every parameter is the same


Question 15 :

What is the data provider name to connect to Access database?

Answer :

Microsoft.Access.

 


Question 16 :

What's the difference between an interface and abstract class?

Answer :

In an interface class, all methods are abstract - there is no implementation. In an abstract class some methods can be concrete. In an interface class, no accessibility modifiers are allowed. An abstract class may have accessibility modifiers.


Question 17 :

What is the difference between a Struct and a Class?

Answer :

Struts are value-type variables and are thus saved on the stack, additional overhead but faster retrieval. Another difference is that struts cannot inherit.


Question 18 :

What's the implicit name of the parameter that gets passed into the set method/property of a class?

Answer :

Value. The data type of the value parameter is defined by whatever data type the property is declared as.


Question 19 :

What does the keyword "virtual" declare for a method or property?

Answer :

The method or property can be overridden.


Question 20 :

How is method overriding different from method overloading?

Answer :

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.


Question 21 :

Describe the accessibility modifier "protected internal"?

Answer :

It is available to classes that are within the same assembly and derived from the specified base class.


Question 22 :

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

Answer :

System.Object.

 


Question 23 :

What does the term immutable mean?

Answer :

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.


Question 24 :

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

Answer :

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


Question 25 :

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

Answer :

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.