| C# Question Answers-41 - 50 | C# Question Answers-61 - 70 | |
C# (C-Sharp) Question Answers - 51-60 |
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
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();
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
// 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();
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.
Objects are the basic run-time entities in an object-oriented system. They contain data and methods that define meaningful operations.
LINQ extension methods that return a boolean value are called Quantifiers.
Access modifiers specify the declared accessibility of a member or type:
| C# Question Answers-41 - 50 | C# Question Answers-61 - 70 | |