1. Allow from one case to another in C++ to allow tags to run from one case
for example:
int a =1;
switch(a)
{
case 1:
some statements;
case 2;
other statements;
break;
}
There can be no break between the first case and the second case
And in C# this break cannot be omitted.
and isOnly the type of the type to be converted will be detected, and no other operations will be performed. Therefore, the conversion can be successful only if the conversion type is a target type or a subclass of the target type (of course, if the target type is an interface, and the type to be converted can be implemented).
Force conversion will call system definitions (such as float conversion to int type) or conversion functions defined by yourself (defined through implicit operator).
From the perspective of conventional requirements, in most cases, it is recommended to use as conversion.
But be careful that as cannot be used for value type. Because when the conversion is unsuccessful, the value type cannot be
null. You can consider using is.
object o = 1;
int i = 0;
if(o is int)
i = (int)o;
It is a compile time constant
readonly is a runtime constant
It is recommended to use static readonly instead of const
The const variable is hardcoded into assembly, and there is a version problem.
C# only replaces fianl with sealed. In addition, C#'s derived classes will not override the base class method by default. It will only override when the base class method uses the keyword virtual flag and the subclass explicitly uses the override flag method. This also effectively solves the version problem.
It is essentially a function pointer.
Because the commission also triggered a debate between sun and MS.
Anders introduced delegate in Delphi as early as Borland, and later went to MS and added delegate when they got J#. Sun was very dissatisfied and said that they destroyed the features of the Java language.
When it comes to C#, Anders can finally add delegate legitimately.
-------------------------------
Dogtail Continues with mink (II)
protectedTake the union of internal and protected, that is, in the same Assembly or different
All are accessible within the Assembly but inherited from this class.
3..NET FrameWork uses string buffer pooling technology.
For example, string a = "test"; and string b = "test"; a and b point to the same area in memory.
In addition, there are several ways to judge and other judgment in C#. You can refer to the following rules for rewriting:
public static bool ReferenceEquals( object left, object right );
public static bool Equals ( object left, object right );
public virtual bool Equals( object right);
public static bool operator==( MyClass left, MyClass right );
The above four methods override should follow the following rules:
1. Don't override the first two anyway
2. The third and fourth ones should be rewritten when the custom type is ValueType
3. When the custom type is ReferenceType, if you want to change the default method of ReferenceType using object flags, you can rewrite the third one
4. When the custom type is RefrenceType, it is best not to rewrite operator==.
The reasons for the above rules are given below:
1.
A. The ReferenceEquals() method is equal if and only when two variables point to the same object. Its implementation is the comparison object flag (Object Identity, any object will generate an OI when it is created). Therefore, the result obtained by comparing any value type with ReferenceEquals() is false, even if it is compared with yourself, because it is necessary to pack, the boxing result is definitely not the same object. Therefore, there is no need to rewrite this method. ReferenceEquals() of object is the best way to achieve this comparison.
B. Equals (object left, object right) This method is used when you don’t know what type of parameters to be compared. So how do you compare whether two variables are equal? It is very simple to hand over the Equals method of the variables to be compared. The implementation of the above method is roughly as follows:
public static bool Equals( object left, object right )
{
// Check object identity
if (left == right )
return true;
// both null references handled above
if ((left == null) || (right == null))
return false;
return (right);
}
The reason why we don’t rewrite the above two methods is because both of them realize their own semantics well.
2. Why do we need to rewrite the third method when it is value?
ValueType is the base class of all value types. Since it wants to implement the semantics of equal values, it rewrites the Equals method of object. However, without knowing the derived class variables and their types, ValueType's Equals can only be compared with reflection when implementing it. This efficiency is very low, so any custom value type must be rewrite Equals to avoid the efficiency loss of reflection caused by the default implementation of ValueType.
3. The default implementation of the instance Equals method of object is the same as ReferenceEqual. It is achieved by comparing object flags. Some ReferenceTypes require the semantics of equal values, such as string, which requires rewriting the instance Equals.
4. .Net FrameWork expects that all ReferenceType operators == retain the quotation semantics.
Equality must satisfy three points in semantic meaning
1. Reflexive a=a must be established forever;
2. Symmetry a=b then b=a;
3. Pass a=b;b=c then a=c
public class Foo
{
public override bool Equals( object right )
{
//1. Determine whether the reference to the variable to be compared is empty
if (right == null)
return false;
//2. Whether to point to the same instance, if it is the same instance, it must be equal.
if (( this, right ))
return true;
//3. Determine whether the types are the same
if (() != ())
return false;
// 4. Compare content
return CompareFooMembers(
this, right as Foo );
}
}
The third step is, can't it be possible to convert it into an object referenced by this?
The answer is no, it must be of the same type. As an example:
public class B
{
public override bool Equals( object right )
{
// check null:
if (right == null)
return false;
// Check reference equality:
if (( this, right ))
return true;
// Problems here, discussed below.
B rightAsB = right as B;
if (rightAsB == null)
return false;
return CompareBMembers( this, rightAsB );
}
}
public class D : B
{
// etc.
public override bool Equals( object right )
{
// check null:
if (right == null)
return false;
if (( this, right ))
return true;
// Problems here.
D rightAsD = right as D;
if (rightAsD == null)
return false;
if (( rightAsD ) == false)
return false;
return CompareDMembers( this, rightAsD );
}
}
//Test:
B baseObject = new B();
D derivedObject = new D();
// Comparison 1.
if ((derivedObject))
( "Equals" );
else
( "Not Equal" );
// Comparison 2.
if ((baseObject))
( "Equals" );
else
( "Not Equal" );
Pay attention to the bold part and know that if the elements compared to B's CompareBMembers and D's CompareDMembers are equal, Compare 1 will output Equals, and Compare 2 will always output Not Equal. So in this case, symmetry in equal semantics will be violated.
So be honest and check the third step.
In the above example, D is one more sentence than B:
if (( rightAsD ) == false)
return false;
It is obvious that the elements of the base class must be allowed to judge whether they are equal. However, if the direct base class of a certain class is an object, it must be called, otherwise it will be the same as not rewriting the instance Equals.
4. Unlike C++ references, ref and out are actually pointers, and the copy value is passed between functions.