C Sharp

1. An Overview of C# 2. OOP 3. C# Environment 4. IDE 5. Basic Requirement 6. Comments 7. My first program 8. Line Break 9. Escape Sequence 10. Identifiers 11. Variables 12. Datatypes 13. Range of datatypes in C# 14. Declare Variables 15. Reserved Key Words 16. Console.Write() 17. Chained & Embedded state 18. Prog.to print sum of 2 nos 19. Console.ReadLine() 20. Prg.to print sum & average 21. Area & Perimeter of Rectangle 22. Area Circumference of Circle 23. Prg.for Simple Calculator 24. Operatos in C# 25. Ex. Of Operators 26. Prg. to Swap two numbers 27. Hierarchy of Operators 28. if( ) statement 29. Prg.to check +ve,-ve or zero 30. if..else statement 31. Prg.to check Odd or Even 32. Prg.to print larger of 2 nos 33. Prg.to print largest of 3 nos 34. Prg.to check divisibility 35. Prg.to check print range 36. for. . . loop statement 37. prg.to print series of nos 38. Prg.to check divisibility 39. prg.to print sum of series 40. Prg.to print Pyramid 41. prg.for Factorial 42. prg.to fill screen 43. prg.for largest/smallest no 44. prg.to print reverse no 45. prg.to add each digit 46. prg.for sum of Factorial 47. prg.for fibonacci series 48. prg.to check Prime nos 49. prg. Prime no from 1 to 100 50. prg.Specified Prime nos 51. while( ) statement 52. do. . While statement 53. goto statement 54. break & continue statement 55. switch case statement 56. Array in C# 57. print reverse order using array 58. Ascending/Descending order 59. prg.to search no in Array 60. Multidimensional arrays 61. String in C# 62. String with spaces 63. prg.to print string in reverse 64. prg.to count A,E,I,O,U vowel 65. User Define Methods 66. Methods program example 67. General Purpose Programs 68. Loan & EMI calculation prg. 69. Table print prg. 70. Leap year program 71. lower to UPPER case prg. 72. Age Distribution prg. 73. Bank note calculation prg. 74. Simple Interest prg. 75. Compound Interest prg. 76. Simple Depreciation prg. 77. Reducing bal.Depreciation prg. 78. Marksheet prg. 79. Income Tax prg. 80. Time calculator prg. 81. Distance converter prg. 82. Volume Air Calculation prg. 83. Time to fill Water Tank prg. 84. Salary Calculation prg. 85. Total Sale Calculation prg. 86. Male/Female pecentage prg. 87. Library Rent prg. 88. Office Expance prg. 89. Total Salary Calculation prg. 90. Profit or Loss prg. 91. Total Profit/Loss prg. 92. Virtual Function . 93. C# Interview Part1 94. C# Interview Part2 95. C# Interview Part3 96. C# Interview Part4 97. C# Interview Part5 98. C# Interview Part6 99. C# Interview Part7 100. C# Interview Part8 101. C# Interview Part9 102. C# Interview Part10 103. C# Interview Part11 104. C# Interview Part12 105. C# Interview Part13 106. C# Interview Part14 107. C# Interview Part15
Pr.Pg Next Pg

C# Interview Questions and Answers Part 11


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",



int fileOffset = 0;

while(fileOffset < myFile.Length)






  • 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;



conn = new Connection();





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





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



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.

Pr.Pg border                                              Next Pg