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 1


1. What's C# ?

  • C# is an object-oriented programming language developed by Microsoft Corporation.

  • C# is intended to be a simple, modern, general-purpose, object-oriented programming language. Its development team is led by Anders Hejlsberg. The most recent version is C# 5.0, which was released on August 15, 2012.

  • C# source code as well as those of other .NET languages is compiled into an intermediate byte code called Microsoft Intermediate Language. C# is primarily derived from the C, C++, and Java programming languages with some features of Microsoft's Visual Basic in the mix.

  • C# is used to develop applications for the Microsoft .NET environment. .NET offers an alternative to Java development. Microsoft's Visual Studio .NET development environment incorporates several different languages including VB.NET, C#, C++, and J# (Microsoft Java for .NET), all of which compile to the Common Language Runtime.

  • C# is designed for Common Language Infrastructure (CLI), which consists of the executable code and runtime environment that allows use of various high-level languages to be used on different computer platforms and architectures.



2. Features of C#:

  • C# is the programming language that most directly reflects the underlying Common Language Infrastructure (CLI) Most of its intrinsic types correspond to value-types implemented by the CLI framework.

  • However, the language specification does not state the code generation requirements of the compiler that is, it does not state that a C# compiler must target a Common Language Runtime, or generate Common Intermediate Language (CIL), or generate any other specific format.

  • Theoretically, a C# compiler could generate machine code like traditional compilers of C++ or Fortran. Some notable features of C# is below:

  • C# supports strongly typed implicit variable declarations with the keyword var.

  • Meta programming via C# attributes is part of the language.

  • C# has strongly typed and verbose function pointer support via the keyword delegate.

  • C# offers Java-like synchronized method calls, via the attribute.

  • The C# languages does not allow for global variables or functions.

  • A C# namespace provides the same level of code isolation as a Java package or a C++ namespace.

  • Implicitly typed local variables.


3. What is an object?

  • An object is an instance of a class through which we access the methods of that class. “New” keyword is used to create an object.

  • A class that creates an object in memory will contain the information about the methods, variables and behavior of that class.



4. What is the difference between public, static and void?

  • All these are access modifiers in C#. Public declared variables or methods are accessible anywhere in the application.

  • Static declared variables or methods are globally accessible without creating an instance of the class. The compiler stores the address of the method as the entry point and uses this information to begin execution before any objects are created.

  • And Void is a type modifier that states that the method or variable does not return any value.



5. 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:


using System;

class main


public static void Main()




Console.WriteLine(\"In Try block\");





Console.WriteLine(\"In Finally block\");





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



6.What are the types of comment in C# with examples?

i. Single line




//This is a Single line comment


ii. Multiple line (/* */)




/*This is a multiple line comment

We are in line 2

Last line of comment*/


iii. XML Comments (///).




/// <summary>

/// Set error message for multilingual language.

/// </summary>


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


using System;

public class StringTest


public static void Main(string[] args)


Object nullObj = null; Object realObj = new StringTest();

int i = 10;

Console.WriteLine(\"Null Object is [\" + nullObj + \"]\n\"

+ \"Real Object is [\" + realObj + \"]\n\"

+ \"i is [\" + i + \"]\n\");

// Show string equality operators

string str1 = \"foo\";

string str2 = \"bar\";

string str3 = \"bar\";

Console.WriteLine(\"{0} == {1} ? {2}\", str1, str2, str1 == str2 );

Console.WriteLine(\"{0} == {1} ? {2}\", str2, str3, str2 == str3 );





Null Object is []

Real Object is [StringTest]

i is [10]

foo == bar ? False

bar == bar ? True


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


9.How do you mark a method obsolete?

  • [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 always capitalized.


10.How do you implement thread synchronization (Object.Wait, Notify,and CriticalSection) in C#?

  • You want the lock statement, which is the same as Monitor Enter/Exit:

  • lock(obj) { // code }


try {


// code






Pr.Pg border                                              Next Pg