Feel the first day of writing essays encountered difficulties, feel good time ah, back to the dorm is more than 8 points, and then only to read a little book. After writing essays, first have to recall, then organize, and finally type. There is the dormitory ushered in the day of the network, especially inconvenient, only the next day to the company hair. I am currently a. NET development intern, before fragmented learned some. NET knowledge, now intends to consolidate under C #, read the "C # Advanced Programming" After the feeling of knowledge is huge, there are many branches did not grasp, although do not know some things worth learning, or learn it first.
Okay, get to the point: starting with the 7th C # Advanced programming operator and type casting
Type of security
Type conversions
First look at a piece of code:
Byte value1=10;
Byte value2=20;
Byte total=value1+value2;
Console.WriteLine (total);
At first glance there seems to be no error, but at this point the VS will pop up the following error message:
Error: Cannot implicitly convert type "int" to "byte". An explicit conversion exists (is there a missing cast?)
This is because byte can only be 8-bit, two byte-type addition, it is easy to get a value beyond the 8-bit size, which reflects the strong security of C # type;
At this point, the byte total=value1+value2;
Change to byte Value3 = (byte) (value1 + value2); Show transformations
or int value3 = value1 + value2; Hermit conversion
There are two types of conversions, implicit conversions and explicit conversions .
Implicit conversions:
is to convert from a smaller data type to a larger data type
Attention:
Unsigned variables can be converted to signed variables, as long as unsigned variable values are within the value of the signed variable
• For nullable types in a type conversion (with "?" after the type character), the nullable type is the type that is allowed to be null:
• Nullable type conversion rules follow conversion rules for non-nullable types, that is, smaller types convert to larger types
• A non-nullable type can be implicitly converted to a nullable type, but a nullable type cannot be implicitly converted to a non-nullable type. This is because nullable type values are allowed to be null, but non-nullable types are not allowed to be null.
An explicit conversion:
is the cast, and the problem is the numerical overflow.
For example:
Long val=3000000000;
int i= (int) Val;
Arithmetic overflow, can be detected with unchecked and checked
By default is unchecked, that is, the value overflow does not detect
Using checked will force the runtime to throw an exception:
Long val=3000000000;
int i=checked ((int) val);
or checked use {} to wrap the statement that needs to verify overflow
Checked
{
int i=checked ((int) val);
}
Exception handling is also introduced later with exception try}{} catch{}
As mentioned earlier, nullable types are converted to non-nullable types, and display transformations must be used. such as int? Convert to int. This is because nullable types are allowed to be null.
If the nullable type is null, the conversion to a non-nullable type throws an exception.
Int? A=null;
int b= (int) A; Throws an exception
If you need to convert between numbers and strings, you can try. NET class Library: ToString ();
int i=10;
String s=i.tostring ();
About type conversions,. NET also provides two methods of seat belt conversion: the Parse () and the Convert method.
For example:
String somestring = "22";
int value1 = Int32.Parse (somestring);
int value2 = Convert.ToInt32 (somestring);
Also mentioned a point, is the packing and unpacking .
Boxing is the conversion of a value type to a reference-type object type. Unpacking is the conversion of the object type to a value type.
Cases:
int myint=10;
Object Myobject=myint; Boxing, value types can be converted to reference object types
int myint2= (int) myObject; The reverse process of unpacking, boxing, requires casting
Boxing, unpacking can be used for: for example, a method needs to reference a type parameter, at this point the value type is boxed into a reference type, then can be disassembled as a value type.
23:37, today first wrote this, because the reading time itself is relatively late, then write the progress of the essay is relatively slow, far behind the progress of reading. Left a little tail, it's about comparing the equality between objects .
Go ahead and write the overload of the operator tomorrow.
Later will strive to write a little C # learning content sharing, but also hope that we work together, C # to flourish, haha. Welcome to teach!
Learn about the security of "C # Advanced Programming" type