Twenty-Five Basic Concepts of C# 2

by qaswr820 on 2011-06-13 12:22:44

6. What does the sealed modifier do?

Answer:

The sealed modifier indicates sealing.

When used on a class, it means the class cannot be inherited anymore. It cannot be used together with abstract because these two modifiers are mutually exclusive in meaning.

When used on methods or properties, it means that the method or property cannot be overridden anymore and must be used together with the override keyword because the sealed method or property must correspond to a virtual member in the base class.

It is typically used when implementing third-party libraries where you don't want clients to inherit from them, or for classes that do not need further inheritance to avoid abuse of inheritance causing a messy hierarchy system.

Proper use of the sealed modifier can also improve runtime efficiency since there is no need to consider overriding by subclasses.

7. What is the difference between override and overload?

Answer:

override means rewriting and is used in derived classes to implement virtual members from the base class.

overload means overloading and is used within the same class to implement methods with the same name but different parameters (different types or numbers).

Example omitted.

8. What is an indexer?

Answer:

A class that implements an indexer can be used like an array, but unlike arrays, indexers are not limited to int parameters.

In short, it is essentially a parameterized property.

Example omitted.

9. What is the role of the new modifier?

Answer:

The new modifier is a different concept from the new operator.

The new modifier is used to declare a class or member of a class, indicating that it hides a member with the same name in the base class. The new operator is used to instantiate a type.

The new modifier can only be used in derived classes, usually to supplement deficiencies in the base class design.

The new modifier and the override modifier cannot be used simultaneously on a member because they are mutually exclusive in meaning.

Example omitted.

10. What is the meaning of the this keyword?

Answer:

this is a reserved word that can only be used in constructors and method members.

In the class constructor, it refers to the object currently being constructed. In a method, it refers to the object that called the method. In a structure's constructor, it refers to the structure being constructed. In a structure's method, it refers to the result of calling the method.

The this keyword cannot be used in static member implementations because at that point the object or structure has not been instantiated.

In C#, this is actually a constant, so operations like this++ cannot be performed.

The this keyword is generally used to qualify hidden members with the same name, pass the object itself as a parameter, declare indexers, and check if the passed parameter object is itself.

Example omitted.

(Note: Some examples were omitted for brevity, but the core explanations remain intact.)