Главная | Обратная связь | Поможем написать вашу работу!
МегаЛекции

Override & Virtual Classes




Readonly

● In a field declaration, readonly indicates that assignment to the field can only occur as part of the declaration or in a constructor in the same class

Immutable after assignment: Unlike a const which must have its value defined at compile-time, a readonly can have its value set once upon instantiation (either at the time it is declared or in a constructor), but thereafter it is immutable.

If I’m readonly, so are all of you: If a Struct is marked as readonly, all of its fields must also be marked readonly either explicitly or through auto-properties:

Explicit: public readonly double X;

Auto-property: public double X { get; }

https: //docs. microsoft. com/en-us/dotnet/csharp/language-reference/keywords/readonly

Const

Immutable: Constant fields and locals aren't variables and may not be modified.

Not variables: Since a “const” cannot be modified, it is technically incorrect to refer to it as a “variable” since it cannot vary.

Direct Access (without an instance): Const fields behave as though they were static and can be accessed by referencing the Class name without having to declare an instance of the class;
Float Tau = Mathf. PI * 2;

Const cannot be static: The static modifier is not allowed in a constant declaration

https: //docs. microsoft. com/en-us/dotnet/csharp/language-reference/keywords/const

Static

● Use the static modifier to declare a static member, which belongs to the type itself rather than to a specific object. The static modifier can be used with classes, fields, methods, properties, operators, events, and constructors, but it cannot be used with indexers, finalizers, or types other than classes

A static member cannot be referenced through an instance. Instead, it is referenced through the type name

Non-Instanced Classes / Properties: In contrast to typical classes which you must instantiate instances of with the use of the new Keyword, Static classes and their member variables are accessed without referencing an instance of the class

○ String translatedString = Locale. Translate(originalString);

If I’m Static, so are all of you: A class which is marked as Static must have all its methods and member variables marked as static as well

Mixed Static: A class that is not static is allowed to have some properties and methods which are static and some which are not

One field, many faces: No matter how many instances of the class are created, only one instance of the static variable exists.

Accessed by name: To access this static variable, you use the class name, rather than referencing an instance of the class;
String greeting = Locale. greeting;

Keep count: Static member variables of non-static classes can be used to keep count of the number of instances which exist of the class

https: //docs. microsoft. com/en-us/dotnet/csharp/language-reference/keywords/static

https: //docs. microsoft. com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-classes-and-static-class-members

Override & Virtual Classes

Override keyword:

○ The override modifier is required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event.

Child overrides Parent: The override keyword is used when a derived class needs to override the definition of an abstract or virtual method

https: //docs. microsoft. com/en-us/dotnet/csharp/language-reference/keywords/override

Override ToString(): Should you wish to provide a custom return for the ToString method, you can override it in your class:
public override string ToString(){

}

■ https: //docs. microsoft. com/en-us/dotnet/csharp/programming-guide/classes-and-structs/how-to-override-the-tostring-method

Virtual keyword:

○ The virtual keyword is used to modify a method, property, indexer, or event declaration and allow for it to be overridden in a derived class

You can override me: Marking a method or variable with the virtual keyword allows it to be overridden in derived classes, but it is not mandatory for the behaviour to be overwritten as it would be if the method was declared abstract

https: //docs. microsoft. com/en-us/dotnet/csharp/language-reference/keywords/virtual

Abstract & Sealed

https: //docs. microsoft. com/en-us/dotnet/csharp/language-reference/keywords/using-directive

Abstract keyword:

○ The abstract modifier indicates that the thing being modified has a missing or incomplete implementation

I trust you’ll finish this before using it: The abstract keyword may be used to mark a Class or Method as incomplete, primarily for use in a base (parent) class in anticipation of its derived (child) class finishing the implementation.

Don’t try to use it: Since abstract classes are incomplete in nature, you will get a compile error if you try to create an instance of one.

○ Abstract Classes: A class which may contain properties and methods who are not fully defined

○ Abstract Methods: A method defined within a class (the class itself may or may not be abstract) which is incomplete in its definition.

■ To complete the implementation of a Method in a derived class, use the “override” keyword

https: //docs. microsoft. com/en-us/dotnet/csharp/language-reference/keywords/abstract

Sealed keyword:

Can’t inherit from sealed classes: When applied to a class, the sealed modifier prevents other classes from inheriting from it.

Children can declare themselves as sealed: A derived class can be set as sealed, essentially preventing any further derivation.

■ To accomplish this, but the “sealed” keyword before the “override” keyword” in the class member definition

Opposite of Abstract: The sealed keyword is mutually exclusive from the abstract keyword because “abstract” denotes a class which must be defined through its derived classes, while “sealed” precludes the possibility.

https: //docs. microsoft. com/en-us/dotnet/csharp/language-reference/keywords/sealed

Поделиться:





Воспользуйтесь поиском по сайту:



©2015 - 2024 megalektsii.ru Все авторские права принадлежат авторам лекционных материалов. Обратная связь с нами...