13th Zhang Yi type and bit mark [". NET Framework Program design" Reading notes]

Source: Internet
Author: User
Tags define bool constant numeric value string format tostring
. NET Framework | notes | procedure | Design 13th Zhang Yi type and bit mark

One, enum type

1, the use of enumerated types of reasons:

L enumeration type is to make the program easier to write, read, maintain, in the code in the use of symbolic names to replace the number is a consistent idea of programming.

L strongly typed, easy to type test

2, Attention matters:

L Enum type inherits from System.enum,system.enum and inherits from System.valurtype

L Enumeration types cannot define methods, properties, events

L enumeration types are constants rather than read-only fields, so it is possible to introduce version problems (see chapter eighth for a discussion)

L Define the enumeration type on the same level as the type that references it, reducing the amount of code entry



3, System.Enum in the application of the method:

L public static type Getunderlyingtype (type enumtype);
Gets the underlying type for saving an instance of an enumerated type. The underlying type syntax used to declare an enumeration type is as follows:
Enum Human:byte
{
Male,
Female
}
The above method is invoked Enum.getunderlyingtype (typeof (Human)) and will be returned to System.Byte;
L public override string ToString ();
public string ToString (string); parameter is a format string
public static string format (Type enumtype,object value,string format);
value– the value to convert, format– format string (g,g,x,x,d,d,f,f)
L public static Array GetValues (Type enumtype);
Gets an array of constant values in the enumeration
L public static string GetName (Type enumtype,object value);
Retrieves the name of a constant with the specified value in the specified enumeration
L public static string[] GetNames (Type enumtype);
Retrieves an array of constant names in the specified enumeration.
L public static Object Parse (Type, String);
public static Object Parse (Type, string, bool);
Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumeration object
L public static bool IsDefined (Type enumtype,object value);
Returns the indication of whether a constant with the specified value exists in the specified enumeration, value or name of the constant
L Series Toobject method
Returns an instance of the specified enumeration type set to the specified value
Second, bit mark

L Use the System.flagsattributes customization feature so that the ToString or Format method can look for each matching symbol in an enumerated value, concatenate them into a string, and separate them with commas; The parse method can split a string and get a composite enumeration type by using the attribute

l use the format string F or F to have the same effect

The following example illustrates the above situation

Using System;



[Flags]//Custom Features

public enum Human:byte//custom base type

{

Male = 0x01,

Female = 0x10

}



public class Enumtest

{

public static void Main ()

{

Human Human = Human.male | Human.female; Hybrid?



Console.WriteLine (Human. ToString ()); Conditions for customizing attributes using Flags

Console.WriteLine (Human. ToString ("F")); No custom features are used with flags



Console.WriteLine (Enum.format (typeof (Human), Human, "G"))//Use flags to customize features

Console.WriteLine (Enum.format (typeof (Human), Human, "F"))//No use of flags custom features



Human = (human) Enum.parse (typeof (Human), "17");

Console.WriteLine (Human. ToString ()); Conditions for customizing attributes using Flags

Console.WriteLine (Human. ToString ("F")); No custom features are used with flags

}

}



/* Run Results

Male, Female

Male, Female

Male, Female

*/

Note: The annotations in the above program are syntax when the flags feature is not used


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.