The class LimitedInt
{
Const int MaxValue=https://bbs.csdn.net/topics/100;
Const int MinValue=https://bbs.csdn.net/topics/0;
Public static implicit operator int (LimitedInt li)
{
Return li. TheValue;
}
Public static implicit operator LimitedInt (LimitedInt li)
{
LimitedInt li=new LimitedInt ();
li.TheValue=https://bbs.csdn.net/topics/x;
Return li;
}
Private int _theValue=https://bbs.csdn.net/topics/0;
Public int TheValue
{
The get {return _theValue; }
Set
{
If (value
_theValue=https://bbs.csdn.net/topics/0;
The else
_theValue=https://bbs.csdn.net/topics/value> MaxValue? MaxValue: value;
}
}
}
Class Program
{
The static void Main ()
{
LimitedInt li=500;
Int value=https://bbs.csdn.net/topics/li;
Console. WriteLine (" value: li: {0}, {1} ", li, TheValue of the value)
}
}
CodePudding user response:
Public static implicit operator LimitedInt (int x)
{
LimitedInt li=new LimitedInt ();
li.TheValue=https://bbs.csdn.net/topics/x;
Return li;
}
CodePudding user response:
reference 1st floor weixin_43060553 response: public static implicit operator LimitedInt (int x) { LimitedInt li=new LimitedInt (); li.TheValue=https://bbs.csdn.net/topics/x; Return li; } The corresponding code above part to write wrong CodePudding user response:
I understand a little bit, int value=https://bbs.csdn.net/topics/li; The equivalent of the li calls object class assigned to the value; So in the Console. WriteLine input value is equivalent to call again when li, the class of the object CodePudding user response:
The static implicit operator int (LimitedInt li) Is that you can convert an int to LimitedInt type, If there is no this implicit conversion, int and LimitedInt is not suitable, two types of equal sign cannot be directly used to assignment, CodePudding user response:
The reference 4 floor github_36000833 reply: static implicit operator int (LimitedInt li) Is that you can convert an int to LimitedInt type, If there is no this implicit conversion, int and LimitedInt is not suitable, two types of the equal sign cannot be directly used to assignment, Oh, oh, is this meaning ah CodePudding user response:
Don't explain, baidu "c # operator overloading" by c # CodePudding user response:
refer to 6th floor wanghui0380 response: don't explain, to baidu "c # operator overloading" c # This is not overloaded ok ~ _ ~ CodePudding user response:
Oh oh oh oh oh oh, see, CodePudding user response:
refer to 6th floor wanghui0380 response: don't explain, to baidu "c # operator overloading" c # In fact I ask not what you said this question, I want to ask how is the value of using LimitedInt LimitedInt methods of this class CodePudding user response:
Type conversion (Cast operator) is a kind of operator, Int I= 123.4 (int);//here (int) is a type conversion operator When there is a display conversion, The class LimitedInt { Public int I; Public static explicit operator LimitedInt (int I) { Return new LimitedInt () {I=I}; } } We can write LimitedInt li= (LimitedInt) 123;//here (LimitedInt) is also a type conversion operator Is used in the example of the building Lord implicit operator (implicit conversion), can need not write (LimitedInt), but essentially the operation also type conversion operations, CodePudding user response:
The references to the tenth floor github_36000833 response: type conversion (Cast operator) is a kind of operator, Int I= 123.4 (int);//here (int) is a type conversion operator When there is a display conversion, The class LimitedInt { Public int I; Public static explicit operator LimitedInt (int I) { Return new LimitedInt () {I=I}; } } We can write LimitedInt li= (LimitedInt) 123;//here (LimitedInt) is also a type conversion operator Is used in the example of the building Lord implicit operator (implicit conversion), can need not write (LimitedInt), but essentially the operation also type conversion operations, Then it means that LimitedInt assigned to the value, when printing the value, equivalent to again use LimitedInt class? CodePudding user response:
11 references weixin_43060553 response: ... Then it means that LimitedInt assigned to the value, when printing the value, equivalent to again use LimitedInt class? Int I=(int) 123.4; Analogy this example 123.4 assigned to the I, print the I, and not use 123.4, CodePudding user response:
Overloaded operators, it is you make rules, On both sides of the type operating according to the rules, CodePudding user response:
Operator overloading, that is, can be used to direct operation between two objects, CodePudding user response:
nullnullnullnullnullnullnullnullnull