Key Words Flashcards
Override
When inheriting from a base object, the override explicitly allows the inheritor to create its own implementation of the member.
Without specifying “override”, the child member is said to HIDE the parent member. Both will be accessible, but care will be needed to ensure the correct member is used.
The base class Object has a ToString() method, yet every object in .NET inherits from Object. If you use MyClass.ToString() (with no ToString() method written for MyClass), the base class method is executed. You can create an explicit MyClass.ToString() method, but unless “override” is used, the compiler will generate warnings.
Delegate
A class / wrapper for a method, which can then be passed to other methods like a parameter.
Serializable
An object is serializable when it’s able to be converted to a series of bytes and persisted on disk (ie. a file, database record, etc). This is useful when the state of an object needs to be maintained for later use. The process of reading bytes from disk and turning then back into the object is called Deserialization.
Abstract
Meant only to be used as a base for other classes. Can not be instantiated directly. Can only be invoked by creating a class that inherits from the abstract base.
Virtual
Anything inheriting from an object containing a virtual member MUST use “override” when overriding that virtual member; hiding is not allowed. The prevents the base class method from being overridden or used.
Sealed
Prevents other objects from inheriting from the sealed object. An object can not both be sealed and abstract.
Yield
Used with the Break and Return keywords. Helps maintain a state engine (a loop in which the iterations can be stopped and resumed at a later time. YIELD RETURN X stops the loop and returns the current value. RETURN BREAK stops the loop without returning a value.
Protected
A protected member is accessible to code inside the class the protected member exists in, and to objects that inherit from it.
If A has a protected int X, and B:A, B.X is accessible from within B, but referencing A.X from within B will generate errors.
Lock
Limits access to the enclosed code to only one thread at a time.
Static
Anything declared as static will not have its data or members modified. Also, static objects do not have to be instantiated; they’re created when the application initialized.
For example, Console.WriteLine() can be called without creating a Console object. A consequence of this is that there can only ever be a single instance of that object.
Checked
Used to encapsulate code in which overflow errors might occur. When a block of code is checked, an exception will be thrown immediately if an overflow occurs.
*int i = int.MaxValue -10; checked { i+= 20; // boom: OverflowException // "Arithmetic operation resulted in an overflow." }*