*
Previous C# Question Answers-41 - 50 C# Question Answers-61 - 70 Next

C# (C-Sharp) Question Answers - 51-60

51. Explain the use of override with new.

The override modifier extends or modifies the abstract or virtual implementation of an inherited member. The new keyword explicitly hides a member inherited from a base class.

// Example with 'new'
class A {
  public virtual void p() { Console.WriteLine("Class -A"); }
}
class B : A {
  public new void p() { Console.WriteLine("Class -B"); }
}
// Output
// Class -B
// Class -A

// Example with 'override'
class A {
  public virtual void p() { Console.WriteLine("Class -A"); }
}
class B : A {
  public override void p() { Console.WriteLine("Class -B"); }
}
// Output
// Class -B
// Class -B

52. LINQ → List of employees, find the employee name with lowest salary.

var employee = Employees
  .OrderByDescending(e => e.Salary)
  .Skip(1)
  .First();

// If multiple employees share the same salary:
var employees = Employees
  .GroupBy(e => e.Salary)
  .OrderByDescending(g => g.Key)
  .Skip(1)
  .First();

53. List collections available in C#?

  • System.Collections
  • System.Collections.Generic
  • Comparer
  • Dictionary
  • Hashtable
  • List
  • ArrayList
  • LinkedList
  • Queue
  • SortedDictionary
  • SortedList
  • Stack
  • ICollection
  • IComparable
  • IComparer

54. Protected vs. protected internal

protected: Accessible within the class and by derived classes (even in other assemblies).
internal: Accessible within the same assembly.
protected internal: Both rules apply — accessible within the same assembly and also by derived classes in other assemblies.

// Assembly 1
class A {
  protected int foo;
  internal int bar;
  protected internal int baz;
}

class B : A {} // can access: foo, bar, baz
class C {}    // can access: bar, baz

// Assembly 2
class D : A {} // can access: foo, baz
class E {}     // can access: none

55. Remove all instances of a number without knowing index in C#.

// Using LINQ
int[] numbers = { 1, 3, 4, 9, 2 };
int numToRemove = 4;
numbers = numbers.Where(val => val != numToRemove).ToArray();

// Without LINQ
static bool isNotFour(int n) { return n != 4; }
int[] numbers = { 1, 3, 4, 9, 2 };
numbers = Array.FindAll(numbers, isNotFour).ToArray();

// Using Except
int[] numbers = { 1, 3, 4, 9, 2 };
numbers = numbers.Except(new int[]{4}).ToArray();

56. The below code is valid?

Font font = new Font("Arial", 10.0f);
using (font)
{
  // use font here
}
// outside the using block
float f = font.GetHeight();

No, not valid. Once the using block completes, the object is disposed. Accessing it afterwards throws a runtime exception.

57. What are Objects?

Objects are the basic run-time entities in an object-oriented system. They contain data and methods that define meaningful operations.

58. What are different ways to write LINQ Query?

  • Query Syntax (Expression Query)
  • Method Syntax (Method Invocation)
  • Mixed Syntax (combination of above two)

59. What are Quantifiers in LINQ?

LINQ extension methods that return a boolean value are called Quantifiers.

  • All()
  • Any()
  • Contains()
  • SequenceEqual()

60. What are the Access Modifiers available in C#?

Access modifiers specify the declared accessibility of a member or type:

  • public
  • protected
  • internal
  • private
  • protected internal
Back to Index
Previous C# Question Answers-41 - 50 C# Question Answers-61 - 70 Next
*
*