-
Can I define a type that is an alias of another type (like typedef in C++)?
Not exactly. You can create an alias within a single file with the "using" directive: using System;using Integer = System.Int32; // aliasBut 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.
-
Is there an equivalent of exit() for quitting a C# .NET application?
Yes, you can use System.Environment.Exit(int exitCode) to exit the application or Application.Exit() if it's a Windows Forms app.
-
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 method is obsolete")]public int Foo() {...}Note: The O in Obsolete is capitalized.
-
Does C# support #define for defining global constants?
No. If you want to get something that works like the following C code:#define A 1use 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.
-
Is it possible to have a static indexer in C#?
No. Static indexers are not allowed in C#.
-
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 BlockCatch BlockFinally 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() {try {Console.WriteLine("In Try block");return;}finally {Console.WriteLine("In Finally block");}}}Both "In Try block" and "In Finally block" will be displayed. Whether...
-
Is it possible to restrict the scope of a field/method of a class to the classes in the same namespace?
There is no way to restrict to a namespace. Namespaces are never units of protection. But if you're using assemblies, you can use the 'internal' access modifier to restrict access to only within the assembly.
-
-
Is there any sample C# code for simple threading?
Some sample code follows: 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();}}
-
Does C# support parameterized properties?
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...
-
What is the implicit name of the parameter that gets passed into the class set method?
Value, and its datatype depends on whatever variable we are changing.
-
Which of these operator has the Highest Precedence
A) ( )B) &&C) | |
-
How do you check whether an Object is compatible with Specific Type
A) Is OperatorB) Like OperatorC) Compare operator
-
Which of the escape sequence is used for Backspace
A) oB) aC) b
-
For Each statement implicitly implements which interface
A) IEnumerableB) IcomparerC) NoneExplanation: All iterators implements Ienumerable
-
Array declaration in C# is done with
A) [ ]B) {}C) ( )
-
Which is .NET s answer to Memory Management
A) ReflectionB) Common Type SystemC) GarbageCollection
-
Which of the following keyword is used along with Main function in C#
A) VirtualB) StaticC) InheritsExplanation: Main functions are always static as it runs with out any instance
-
Instantiating a reference object requires the use of
A) New keywordB) = OperatorC) Set keyword
C# Interview Questions
Ans