C#.NET OOP Programming Concepts FAQ
1. 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.

2. does my Windows application pop up a console window every time I run it ?
Make sure that the target type set in the project properties setting is set to Windows Application, and not Console Application. If you’re using the command line, compile with /target:winexe, not /target:exe.

3. Why do I get a syntax error when trying to declare a variable called checked?
The word checked is a keyword in C#.

4. What is the equivalent to regsvr32 and regsvr32 /u a file in .NET development?
RegAsm.exe.

5. Is there regular expression (regex) support available to C# developers?
The .NET class libraries provide support for regular expressions. Look at theSystem.Text.RegularExpressions namespace.

6. What is the difference between Finalize() and Dispose()?
Dispose() is called by as an indication for an object to release any unmanaged resources it has held. Finalize() is used for the same purpose as dispose however finalize doesn’t assure the garbage collection of an object.

7. How does the XmlSerializer work? What ACL permissions does a process using it require?
The XmlSerializer constructor generates a pair of classes derived from XmlSerializationReader and XmlSerializationWriter by analysis of the classes using reflection. Temporary C# files are created and compiled into a temporary assembly and then loaded into a process. The XmlSerializer caches the temporary assemblies on a per-type basis as the code generated like this is expensive. This cached assembly is used after a class is created Therefore the XmlSerialize requires full permissions on the temporary directory which is a user profile temp directory for windows applications.

8. What are circular references?
A circular reference is a run-around wherein the 2 or more resources are interdependent on each other rendering the entire chain of references to be unusable.

9. Explain how to add controls dynamically to the form using C#.NET.?
The following code can be called on some event like page load or onload of some image or even a user action like onclick protected void add_button(Button button) { try { panel1.Controls.Add(button); // Add the control to the container on a page } catch (Exception ex) { label1.Text += ex.Message.ToString(); } } .

10. What are Extender provider components? Explain how to use an extender provider in the project?
An extender provider is a component that provides properties to other components. Implementing an extender provider: • Use the ProvidePropertyAttribute, which specifies the name of the property that an implementer of IExtenderProvider provides to other components, attribute to specify the property provided by your extender provider. • Implement the provided property. • Track which controls receive your provided property. • Implement the IExtenderProvider, which defines the interface for extending properties to other components in a containe, interface.

11. What is the difference between Debug.Write and Trace.Write? When should each be used?
Debug.Write: Debug Mode, Release Mode (used while debuging a project)Trace.write: Release Mode (used in Released verion of Applications)

12. How to achieve polymorphism in C#.NET?
Polymorphism is when a class can be used as more than one type through inheritance. It can be used as its own type, any base types, or any interface type if it implements interfaces. It can be achieved in the following ways: Derived class inherits from a base class and it gains all the methods, fields, properties and events of the base class. To completely take over a class member from a base class, the base class has to declare that member as virtual.

13. How to prevent a class from being inherited in C#.NET?
The sealed modifier is used to prevent derivation from a class. An error occurs if a sealed class is specified as the base class of another class. A sealed class cannot also be an abstract class.

14. What are generics in C#.NET?
Generic types to maximize code reuse, type safety, and performance. They can be used to create collection classes. Generic collection classes in the System.Collections.Generic namespace should be used instead of classes such as ArrayList in the System.Collections namespace.

15. What is the use of GetCommandLineArgs() method in C#.NET?
With GetCommandLineArgs() method, the command line arguments can be accessed.The value returned is an array of strings.

16. What is the difference between const and readonly in C#.NET?
The read only can be modified by the class it is contained in. However, the const cannot be modified. It needs to be instantiated only at the compile time.

17. What is the syntax to inherit from a class in C#?
Public class derivedclass:baseclass.

18. Can you inherit multiple interfaces?
Yes. .NET does support multiple interfaces.

19. Why can’t you specify the accessibility modifier for methods inside the interface?
They all must be public, and are therefore public by default.

20. Describe the accessibility modifier “protected internal”?
It is available to classes that are within the same assembly and derived from the specified base class.

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

22. How do you prevent a class from being inherited?
Mark it as sealed.

23. Is it possible to Override Private Virtual methods?
No, First of all you cannot declare a method as ‘private virtual’.

24. Are all methods virtual in C#?
No. Like C++, methods are non-virtual by default, but can be marked as virtual.

25. How to declare a two-dimensional array in C#?
Syntax for Two Dimensional Array in C Sharp is int[,] ArrayName;.

26. Is there an equivalent of exit() or quiting 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.

27. Does C# support multiple inheritance?
No, use interfaces instead.

28. What is the name of c#.net compiler?
The name of c# complier is .CSC.

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

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

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

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

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

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

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

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

37. What namespaces are necessary to create a localized application?
System.Globalization, System.Resources.

38. What’s the advantage of using System.Text.StringBuilder over System.String?
StringBuilder is more efficient in the cases, where a lot of manipulation is done to the text. Strings are immutable, so each time it’s being operated on, a new instance is created.

39. ) 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 {}.

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

41. Why is it a bad idea to throw your own exceptions?
Well, if at that point you know that an error has occurred, then why not write the proper code to handle that error instead of passing a new Exception object to the catch block? Throwing your own exceptions signifies some design flaws in the project.

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

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

44. Why are there five tracing levels in System.Diagnostics.TraceSwitcher?
The tracing dumps can be quite verbose and for some applications that are constantly running you run the risk of overloading the machine and the hard drive there. Five levels range from None to Verbose, allowing to fine-tune the tracing activities.

45. Can you change the value of a variable while debugging a C# application?
Yes, if you are debugging via Visual Studio.NET, just go to Immediate window.

46. Which class is at the top of .NET class hierarchy?
System.Object.

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

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

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

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

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

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


 
Subscribe Newsletter
Register to the site for free, and subscribe to the newsletter. You will receive new articles and special content not available elsewhere on the site, right into your e-mail box!