13.07.2015 Views

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 17 ClassesThe Console class contains three properties, In, Out, and Error, that represent the standard input, output, anderror devices, respectively. By exposing these members as properties, the Console class can delay theirinitialization until they are actually used. For example, upon first referencing the Out property, as inConsole.Out.WriteLine("hello, world");the underlying TextWriter for the output device is created. But if the application makes no reference to the Inand Error properties, then no objects are created for those devices. end example]17.6.3 Virtual, sealed, override, and abstract accessorsA virtual property declaration specifies that the accessors of the property are virtual. The virtual modifierapplies to both accessors of a read-write property—it is not possible for only one accessor of a read-writeproperty to be virtual.An abstract property declaration specifies that the accessors of the property are virtual, but does not provide anactual implementation of the accessors. Instead, non-abstract derived classes are required to provide their ownimplementation for the accessors by overriding the property. Because an accessor for an abstract propertydeclaration provides no actual implementation, its accessor-body simply consists of a semicolon.A property declaration that includes both the abstract and override modifiers specifies that the property isabstract and overrides a base property. The accessors of such a property are also abstract.Abstract property declarations are only permitted in abstract classes (§17.1.1.1). The accessors of an inheritedvirtual property can be overridden in a derived class by including a property declaration that specifies anoverride directive. This is known as an overriding property declaration. An overriding property declarationdoes not declare a new property. Instead, it simply specializes the implementations of the accessors of an existingvirtual property.An overriding property declaration must specify the exact same accessibility modifiers, type, and name as theinherited property. If the inherited property has only a single accessor (i.e., if the inherited property is read-onlyor write-only), the overriding property must include only that accessor. If the inherited property includes bothaccessors (i.e., if the inherited property is read-write), the overriding property can include either a single accessoror both accessors.An overriding property declaration may include the sealed modifier. Use of this modifier prevents a derivedclass from further overriding the property. The accessors of a sealed property are also sealed.Except for differences in declaration and invocation syntax, virtual, sealed, override, and abstract accessorsbehave exactly like virtual, sealed, override and abstract methods. Specifically, the rules described in §17.5.3,§17.5.4, §17.5.5, and §17.5.6 apply as if accessors were methods of a corresponding form:• A get accessor corresponds to a parameterless method with a return value of the property type and the samemodifiers as the containing property.• A set accessor corresponds to a method with a single value parameter of the property type, a void returntype, and the same modifiers as the containing property.[Example: In the exampleabstract class A{int y;public virtual int X {get { return 0; }}public virtual int Y {get { return y; }set { y = value; }}public abstract int Z { get; set; }}245

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!