I don't mean to ignore it! [C #]

Source: Internet
Author: User

I don't mean to ignore it! [C #]

 

Written by Allen Lee

 

In "I am only responsible for conversion! [C/C ++] This article presents C/C ++ pairs from the perspective of type conversion.ProgramHow high is C #'s trust in programmers?

Let's take a look at section C #Code:

// Code #01

Public   Enum Alignment
{
Left,
Center,
Right
}

Class Program
{
Static   Void Main ()
{
Alignment = (Alignment )( - 1 );
Console. writeline ();

A =   0 ;
Console. writeline ();

A = (Alignment )( 1 );
Console. writeline ();

A = (Alignment )( 2 );
Console. writeline ();

A = (Alignment )( 3 );
Console. writeline ();
}
}

// Output:
//
// -1
// Left
// Center
// Right
// 3

This code can be compiled and run smoothly. From the output, we can see that C # does not prevent you from performing unexpected enumeration conversion. Only you must take responsibility for your actions. If you perform an unexpected conversion, you will get an unexpected output. On the contrary, if the conversion is meaningful, the output will also be meaningful. In addition, 0 is automatically converted to the corresponding Enumeration type.

Now let's look at another section of C # code:

// Code #02

Public   Enum Alignment: Byte
{
Left,
Center,
Right
}

Class Program
{
Static   Void Main ()
{
Alignment=(Alignment )(-1);
Console. writeline ();
}
}

This time, we have specified the underlying type for alignment, and the compiler does not ignore it anymore. It rejects compilation and leaves the following sentence:

Error cs0221: Constant Value '-1' cannot be converted to a 'alignment '(use 'unchecked' syntax to override)

Different from the previous one, programmers should not cause such overflow due to negligence. If you intend to do so, you must use the unchecked syntax to explicitly tell the compiler:

// Code #03
// See Code #02 for alignment.

Class Program
{
Static   Void Main ( String [] ARGs)
{
Unchecked
{
Alignment=(Alignment )(-1);
Console. writeline ();
}
}
}

// Output:
//
// 255

This time, the compiler will not complain. However, you should also think about whether this overflow conversion is really necessary?

So what is the phenomenon of this overflow switch in the C ++ country? Take a look at the following C ++ code:

// Code #04

# Include String >
# Include Iostream >

Enum _ Jb_prog_mode: Unsigned Short
{
Program_one,
Program_two
} ;

Int _ Tmain ( Int Argc, _ tchar * Argv [])
{
// In C, the enum keyword is required
// To declare a variable of Type enumeration.
// In C ++, The enum keyword can be omitted.
// The following expression is legal in C ++ only.

_ Jb_prog_mode e_prog_index = (_ Jb_prog_mode )( - 1 );

STD: cout (UnsignedShort) E_prog_indexSTD: Endl;

Return 0;
}

//Output:
//
//65535

This code can be compiled and run smoothly. From the output results, we can see overflow, but there is no sound in the compiler and runtime. It seems that C ++'s trust in programmers has reached its peak.

Note that ansi c does not allow us to specify the underlying type of enumeration. All underlying types of enumeration are Int.

Next, let's look at the reading of the Registry. Assume that the Registry has the following information:

[HKEY_LOCAL_MACHINE \ SOFTWARE \ Allen]
"Name" = "Allen Lee"
"ID" = DWORD: 00000584

Then let's take a look at the following C # code:

// Code #05

Class Program
{
Static   Void Main ( String [] ARGs)
{
Registrykey rk = Registry. localmachine. opensubkey ( " Software \ Allen " );

Object OBJ = Rk. getvalue ( " Name " );
String Nm = ( String ) OBJ; // Convert_01
// Int Nm = (INT) OBJ; // Convert_02
Console. writeline (Nm );

OBJ = Rk. getvalue ( " ID " );
Int ID = ( Int ) OBJ; // Convert_03
// String id = (string) OBJ; // Convert_04
Console. writeline (ID );
}
}

// Output:
//
// Allen Lee
// 1412

Of course, the above Code can normally output correct results. What if we replace convert_01 and convert_03 with convert_02 and convert_04 respectively? The compiler will not reject your compilation request, but will not hesitate to throw invalidcastexception during the runtime, and points out that the specified type conversion cannot be performed.

So how can c ++ cope with similar situations? If you are interested, refer to "I am only responsible for conversion! The code snippets in the [C/C ++] Article show you how much trust C ++ has in programmers!

Conclusion? Just as the question in this article --

I don't mean to ignore it!

Compared with C/C ++, C # is indeed not so free. However, these restrictions bring us another taste. Are these restrictions of C # A kind of help or binding? This is wise.

 

See also:

    • Allen Lee; I am only responsible for conversion! [C/C ++]
    • Allen Lee; FAQ on enumeration [C #, Il, Bcl]

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.