tailieunhanh - Microsoft Visual C# 2010 Step by Step (P10)
Tham khảo sách 'microsoft visual c# 2010 step by step (p10)', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | 420 Part III Creating Components Operator Constraints You have seen throughout this book that C enables you to overload methods when defining your own types. C also allows you to overload many of the existing operator symbols for your own types although the syntax is slightly different. When you do this the operators you implement automatically fall into a well-defined framework with the following rules You cannot change the precedence and associativity of an operator. The precedence and associativity are based on the operator symbol for example and not on the type for example int on which the operator symbol is being used. Hence the expression a b c is always the same as a b c regardless of the types of a b and c. You cannot change the multiplicity the number of operands of an operator. For example the symbol for multiplication is a binary operator. If you declare a operator for your own type it must be a binary operator. You cannot invent new operator symbols. For example you can t create a new operator symbol such as for raising one number to the power of another number. You d have to create a method for that. You can t change the meaning of operators when applied to built-in types. For example the expression 1 2 has a predefined meaning and you re not allowed to override this meaning. If you could do this things would be too complicated There are some operator symbols that you can t overload. For example you can t overload the dot . operator which indicates access to a class member. Again if you could do this it would lead to unnecessary complexity. Tip You can use indexers to simulate as an operator. Similarly you can use properties to simulate assignment as an operator and you can use delegates to simulate a function call as an operator. Overloaded Operators To define your own operator behavior you must overload a selected operator. You use method-like syntax with a return type and parameters but the name of the method is the keyword operator together with .
đang nạp các trang xem trước