C# (Sharp) Programming Language

  Home  Microsoft .Net Technologies  C# (Sharp) Programming Language


“Learn C# (Sharp) Programming Language by Interview Questions and Answers”



163 C# (Sharp) Programming Language Questions And Answers

23⟩ What are advantages and disadvantages of Microsoft-provided data provider classes in ADO.NET?

SQLServer.NET data provider is high-speed and robust, but requires SQL Server license purchased from Microsoft. OLE-DB.NET is universal for accessing other sources, like Oracle, DB2, Microsoft Access and Informix, but it is a .NET layer on top of OLE layer, so not the fastest thing in the world. ODBC.NET is a deprecated layer provided for backward compatibility to ODBC engines.

 228 views

27⟩ 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 + "] "

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

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

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

}

}

Output:

Null Object is []

Real Object is [StringTest]

i is [10]

foo == bar ? False

bar == bar ? True

 215 views

28⟩ Does C# support #define for defining global constants?

No. If you want to get something that works like the following C code:

#define A 1

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

 211 views

29⟩ 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 or alter the items on the stack without performing unnecessary Push and Pop operations. That is, Stack is implemented as a linked list, but it also provides the convenience of array access.

Indexer declarations are similar to property declarations, with the main differences being that indexers are nameless (the name used in the declaration is this, since this is being indexed) and that indexers include indexing parameters. The indexing parameters are provided between square brackets.

 204 views

30⟩ Does C# support C type macros?

No. C# does not have macros. Keep in mind that what some of the predefined C macros (for example, __LINE__ and __FILE__) give you can also be found in .NET classes like System.Diagnostics (for example, StackTrace and StackFrame), but they'll only work on debug builds.

 216 views

35⟩ What is the wildcard character in SQL?

Let us say you want to query database with LIKE for all employees whose name starts with La. The wildcard character is %, the proper query with LIKE would involve La%.

 233 views

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

FileMode.Open))

{

int fileOffset = 0;

while(fileOffset < myFile.Length)

{

Console.Write((char)myFile.ReadByte());

fileOffset++;

}

}

When myFile leaves the lexical scope of

the using, its dispose method will be called.

 204 views

40⟩ Why do I get an error (CS1006) when trying to declare a method without specifying a return type?

If you leave off the return type on a method declaration, the compiler thinks you are trying to declare a constructor. So if you are trying to declare a method that returns nothing, use void. The following is an example: // This results in a CS1006 error public static staticMethod (mainStatic obj) // This will work as wanted public static void staticMethod (mainStatic obj)

 213 views