Thursday, July 17, 2008

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#? - No. Static indexers are not allowed in C#.

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:

Both “In Try block” and “In Finally block” will be displayed. Whether the return is in the try block or after the try-finally block, performance is not affected either way. The compiler treats it as if the return were outside the try block anyway. If it’s a return without an expression (as it is above), the IL emitted is identical whether the return is inside or outside of the try. If the return has an expression, there’s an extra store/load of the value of the expression (since it has to be computed within the try block).

I was trying to use an “out int” parameter in one of my functions. How should I declare the variable that I am passing to it?

- You should declare the variable as an int, but when you pass it in you must specify it as ‘out’, like the following: int i; foo(out i); where foo is declared as follows: [return-type] foo(out int o) { }

How does one compare strings in C#?

- In the past, you had to call .ToString() on the strings when using the == or != operators to compare the strings’ values. That will still work, but the C# compiler now automatically compares the values instead of the references when the == or != operators are used on string types. If you actually do want to compare references, it can be done as follows: if ((object) str1 == (object) str2) { … } Here’s an example showing how string compares work:

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:

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

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.

2) Which of these string definitions will prevent escaping on backslashes in C#?

1.string s = #.n Test string.;

2.string s = ..n Test string.;

3.string s = @.n Test string.;

4.string s = .n Test string.;

3) Which of these statements correctly declares a two-dimensional array in C#?

1.int[,] myArray;

2.int[][] myArray;

3.int[2] myArray;

4.System.Array[2] myArray;

4) If a method is marked as protected internal who can access it?

1.Classes that are both in the same assembly and derived from the declaring class.

2.Only methods that are in the same class as the method in question.

3.Internal methods can be only be called using reflection.

4.Classes within the same assembly, and classes derived from the declaring class.

5) What is boxing?

a) Encapsulating an object in a value type.

b) Encapsulating a copy of an object in a value type.

c) Encapsulating a value type in an object.

d) Encapsulating a copy of a value type in an object.

6) What compiler switch creates an xml file from the xml comments in the files in an assembly?

1./text

2./doc

3./xml

4./help

7) What is a satellite Assembly?

A peripheral assembly designed to monitor permissions requests from an application.

1.Any DLL file used by an EXE file.

2.An assembly containing localized resources for another assembly.

3.An assembly designed to alter the appearance or .skin. of an application.

8) What is a delegate?

1.A strongly typed function pointer.

2.A light weight thread or process that can call a single method.

3.A reference to an object in a different process.

4.An inter-process message channel.

9) How does assembly versioning in .NET prevent DLL Hell?

1.The runtime checks to see that only one version of an assembly is on the machine at any one time.

2..NET allows assemblies to specify the name AND the version of any assemblies they need to run.

General Questions

2. Describe the accessibility modifier “protected internal”.It is available to derived classes and classes within the same Assembly (and naturally from the base class it’s declared in).

3. What’s the top .NET class that everything is derived from?System.Object.

4. Can you store multiple data types in System. Array? No.

5. How can you sort the elements of the array in descending order?
By calling Sort() and then Reverse() methods.

6. What’s the C# equivalent of C++ catch (…), which was a catch-all statement for 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 {}.

7. Can multiple catch blocks be executed?
No. Once the proper catch code fires off, the control is transferred to the finally block.

8. What is the role of the DataReader class in ADO.NET connections?It returns a read-only dataset from the data source when the command is executed.

Class Questions:

0. How do you inherit from a class in C#?Place a colon and then the name of the base class.

1. Can you prevent your class from being inherited by another class?Yes. The keyword “sealed” will prevent the class from being inherited.

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

3. What’s an abstract class?
A class that cannot be instantiated.Abstract class is an inherited class that have the methods overridden.

4. When do you absolutely have to declare a class as abstract?

When at least one of the methods in the class is abstract.

5. What’s an interface class?
It’s an abstract class with public abstract methods all of which must be implemented in the inherited classes.

6. Why can’t you specify the accessibility modifier for methods inside the interface?
They all must be public.Therefore, to prevent you from getting the false impression that you have any freedom of choice, you are not allowed to specify any accessibility, it’s public by default.

7. Can you inherit multiple interfaces?Yes.

8. What’s the difference between an interface and abstract class?
In an interface no accessibility modifiers are allowed, which is ok in an abstract class.

9. What is the difference between a Struct and a Class?Structs are value-type variables and are thus saved on the stack -> additional overhead but faster retrieval. Another difference is that structs CANNOT inherit. (Questions courtesy of Eyal)

Method and Property Questions

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

11. What does the keyword “virtual” declare for a method or property?The method or property can be overridden.

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

13. Can you override private virtual methods?No. Private methods are not accessible outside the class.

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

14. What are the different ways a method can be overloaded?
Different parameter data types, different number of parameters, different order of parameters.

15. What’s a delegate?A delegate object encapsulates a reference to a method.

16. What’s a multicast delegate?It’s a delegate that points to and eventually fires off several methods.

17. What is a satellite assembly?When you write a multilingual or multi-cultural application in .NET, and want to distribute the core application separately from the localized modules, the localized assemblies that modify the core application are called satellite assemblies.

18. What namespaces are necessary to create a localized application?System.Globalization and System.Resources.

19.What is the difference between and XML documentation tag?Single line code example and multiple-line code example.

20. Is XML case-sensitive? Yes.

Debugging and Testing Questions

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

1. CorDBG – command-line debugger.

2. DbgCLR – graphic debugger.Visual Studio .NET uses the DbgCLR.

22. What does the “This” window show in the debugger?It points to the object that’s pointed to by this reference. Object’s instance data is shown.

23. What does assert() method do?In debug compilation, assert takes in a Boolean condition as a parameter, and shows the error dialog if the condition is false. The program proceeds without any interruption if the condition is true.

24. What’s 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.

25. How do you debug an ASP.NET Web application?Attach the aspnet_wp.exe process to the DbgClr debugger.

26. What are three test cases you should go through in unit testing?

1. Positive test cases (correct data, correct output).

2. Negative test cases (broken or missing data, proper handling).

3. Exception test cases (exceptions are thrown and caught properly).

27. Can you change the value of a variable while debugging a C# application? Yes.

ADO.NET and Database Questions

1. What are advantages and disadvantages of Microsoft-provided data provider classes in ADO.NET?SQLServer.NET data provider is high-speed and robust. OLE-DB.NET is universal for accessing other sources, like Oracle, DB2, Microsoft Access. OLE-DB.NET is not as fastest and efficient as SqlServer.NET.

2. Explain ACID rule of thumb for transactions.A transaction must be:

1. Atomic - It is one unit of work and does not dependent on previous and following transactions.

2. Consistent - Data is either committed or roll back, no “in-between” case where something has been updated and something hasn’t.

3. Isolated - No transaction sees the intermediate results of the current transaction).

4. Durable - The values persist if the data committed even if the system crashes right after.

4. What does the Initial Catalog parameter define in the connection string?The database name to connect to.

5. What is the data provider name to connect to an Access database?Microsoft.Access.

6. What does the Dispose method do with the connection object?Deletes it from the memory.

7. What is a pre-requisite for connection pooling?Multiple processes must agree that they will share the same connection, where every parameter is the same, including the security settings.The connection string must be identical.

Some More C# Questions

1.When you inherit a protected class-level variable, who is it available to? Classes in the same namespace.

2.Are private class-level variables inherited? Yes, but they are not accessible, so looking at it you can honestly say that they are not inherited. But they are.

3.Describe the accessibility modifier protected internal. It’s available to derived classes and classes within the same Assembly (and naturally from the base class it’s declared in).

4.How’s method overriding different from overloading? When overriding, you change the method behavior for a derived class. Overloading simply involves having a method with the same name within the class.

5.What does the keyword virtual mean in the method definition? The method can be over-ridden.

6.Can you declare the override method static while the original method is non-static? No, you can’t, the signature of the virtual method must remain the same, only the keyword virtual is changed to keyword override.

7.How can you overload a method? By using parameters.

8.What’s the advantage of using System.Text.StringBuilder over System.String? StringBuilder is more efficient in the when lot of manipulations have to be done to the text.

9.How can you sort the elements of the array in descending order? By calling Sort() and then Reverse() methods.

10.What’s the .NET datatype that allows the retrieval of data by a unique key? HashTable.

11.What’s class SortedList underneath? A sorted HashTable.

12.Will finally block get executed if the exception had not occurred? Yes.

13.What’s the C# equivalent of C++ catch (…), which was a catch-all statement for 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 {}.

14.What’s a delegate? A delegate object encapsulates a reference to a method. In C++ they were referred to as function pointers.

15.What’s a multicast delegate? It’s a delegate that points to and eventually fires off several methods.

16.How’s the DLL Hell problem solved in .NET? Assembly versioning allows the application to specify not only the library it needs to run (which was available under Win32), but also the version of the assembly.

17.What’s a satellite assembly? When you write a multilingual or multi-cultural application in .NET, and want to distribute the core application separately from the localized modules, the localized assemblies that modify the core application are called satellite assemblies.

19.What’s the difference between and XML documentation tag? Single line code example and multiple-line code example.

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

21.How do you debug an ASP.NET Web application? Attach the aspnet_wp.exe process to the DbgClr debugger.

22.Can you change the value of a variable while debugging a C# application? Yes.

23.What’s the role of the DataReader class in ADO.NET connections? It returns a read-only dataset from the data source when the command is executed.

24.Which one is trusted and which one is untrusted? Windows Authentication is trusted because the username and password are checked with the Active Directory; the SQL Server authentication is untrusted, since SQL Server is the only verifier participating in the transaction.

25.Why would you use untrusted verificaion? Web Services might use it, as well as non-Windows applications.

26.What’s the data provider name to connect to Access database? Microsoft.Access.

27.What does dispose method do with the connection object? Deletes it from the memory.

28.What is a pre-requisite for connection pooling?Multiple processes must agree that they will share the same connection, where every parameter is the same, including the security settings.