X hits on this document

857 views

0 shares

0 downloads

0 comments

263 / 396

C# LANGUAGE SPECIFICATION

conversion operator is not part of the operator’s signature. Thus, a class or struct cannot declare both an implicit and an explicit conversion operator with the same source and target types.

In general, user-defined implicit conversions should be designed to never throw exceptions and never lose information. If a user-defined conversion can give rise to exceptions (for example, because the source argument is out of range) or loss of information (such as discarding high-order bits), then that conversion should be defined as an explicit conversion.

In the example

using System;

public struct Digit { byte value;

public Digit(byte value) { if (value < 0 || value > 9) throw new ArgumentException(); this.value = value; }

public static implicit operator byte(Digit d) { return d.value; }

public static explicit operator Digit(byte b) { return new Digit(b); } }

the conversion from Digit to byte is implicit because it never throws exceptions or loses information, but the conversion from byte to Digit is explicit since Digit can only represent a subset of the possible values of a byte.

10.10 Instance constructors

An instance constructor is a member that implements the actions required to initialize an instance of a class. Instance constructors are declared using constructor-declarations:

constructor-declaration: attributesopt   constructor-modifiersopt   constructor-declarator   constructor-body

constructor-modifiers: constructor-modifier constructor-modifiers   constructor-modifier

constructor-modifier: public protected internal private extern

constructor-declarator: identifier   (   formal-parameter-listopt   )   constructor-initializeropt

constructor-initializer: :   base   (   argument-listopt   ) :   this   (   argument-listopt   )

250Copyright Microsoft Corporation 1999-2003. All Rights Reserved.

Document info
Document views857
Page views857
Page last viewedMon Dec 05 08:59:46 UTC 2016
Pages396
Paragraphs9401
Words133190

Comments