系统之家提供 Windows 系统、Ghost 系统、驱动与常用软件的安全下载及安装教程。纯净系统 原版ISO 微软官方镜像 MSDN我告诉你 系统之家 装机吧 小白一键重装 驱动总裁 万能驱动 启动盘制作 Rufus Ventoy UltraISO PE系统 微PE 进BIOS 设置U盘启动 分区工具 DiskGenius 格式化C盘 激活工具 KMS 正版授权 系统补丁 运行库 DirectX VC++ .NET Framework 安全设置 系统优化 备份还原 后台管理
📢 欢迎访问系统之家!所有资源均经过安全检测。

Boolean Struct (System)

发布时间:2026-09-13 | 浏览:1
📥 下载地址(文章开头)
电脑助手解决一切电脑软件问题。
Access to this page requires authorization. You can try signing in or changing directories . Access to this page requires authorization. You can try changing directories . Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here. Represents a Boolean ( true or false ) value. A Boolean instance can have either of two values: true or false . The Boolean structure provides methods that support the following tasks: Converting Boolean values to strings: ToString Parsing strings to convert them to Boolean values: Parse and TryParse Comparing values: CompareTo and Equals This article explains these tasks and other usage details. Format Boolean values The string representation of a Boolean is either "True" for a true value or "False" for a false value. The string representation of a Boolean value is defined by the read-only TrueString and FalseString fields. You use the ToString method to convert Boolean values to strings. The Boolean structure includes two ToString overloads: the parameterless ToString() method and the ToString(IFormatProvider) method, which includes a parameter that controls formatting. However, because this parameter is ignored, the two overloads produce identical strings. The ToString(IFormatProvider) method does not support culture-sensitive formatting. The following example illustrates formatting with the ToString method. Note that the C# and VB examples use the composite formatting feature, while the F# example uses string interpolation . In both cases the ToString method is called implicitly. Because the Boolean structure can have only two values, it is easy to add custom formatting. For simple custom formatting in which other string literals are substituted for "True" and "False", you can use any conditional evaluation feature supported by your language, such as the conditional operator in C# or the If operator in Visual Basic. The following example uses this technique to format Boolean values as "Yes" and "No" rather than "True" and "False". For more complex custom formatting operations, including culture-sensitive formatting, you can call the String.Format(IFormatProvider, String, Object[]) method and provide an ICustomFormatter implementation. The following example implements the ICustomFormatter and IFormatProvider interfaces to provide culture-sensitive Boolean strings for the English (United States), French (France), and Russian (Russia) cultures. Optionally, you can use resource files to define culture-specific Boolean strings. Convert to and from Boolean values The Boolean structure implements the IConvertible interface. As a result, you can use the Convert class to perform conversions between a Boolean value and any other primitive type in .NET, or you can call the Boolean structure's explicit implementations. However, conversions between a Boolean and the following types are not supported, so the corresponding conversion methods throw an InvalidCastException exception: Conversion between Boolean and Char (the Convert.ToBoolean(Char) and Convert.ToChar(Boolean) methods). Conversion between Boolean and Char (the Convert.ToBoolean(Char) and Convert.ToChar(Boolean) methods). Conversion between Boolean and DateTime (the Convert.ToBoolean(DateTime) and Convert.ToDateTime(Boolean) methods). Conversion between Boolean and DateTime (the Convert.ToBoolean(DateTime) and Convert.ToDateTime(Boolean) methods). All conversions from integral or floating-point numbers to Boolean values convert non-zero values to true and zero values to false . The following example illustrates this by calling selected overloads of the Convert.ToBoolean class. When converting from Boolean to numeric values, the conversion methods of the Convert class convert true to 1 and false to 0. However, Visual Basic conversion functions convert true to either 255 (for conversions to Byte values) or -1 (for all other numeric conversions). The following example converts true to numeric values by using a Convert method, and, in the case of the Visual Basic example, by using the Visual Basic language's own conversion operator. For conversions from Boolean to string values, see the Format Boolean values section. For conversions from strings to Boolean values, see the Parse Boolean values section. Parse Boolean values The Boolean structure includes two static parsing methods, Parse and TryParse , that convert a string to a Boolean value. The string representation of a Boolean value is defined by the case-insensitive equivalents of the values of the TrueString and FalseString fields, which are "True" and "False", respectively. In other words, the only strings that parse successfully are "True", "False", "true", "false", or some mixed-case equivalent. You cannot successfully parse numeric strings such as "0" or "1". Leading or trailing white-space characters are not considered when performing the string comparison. The following example uses the Parse and TryParse methods to parse a number of strings. Note that only the case-insensitive equivalents of "True" and "False" can be successfully parsed. If you're programming in Visual Basic, you can use the CBool function to convert the string representation of a number to a Boolean value. "0" is converted to false , and the string representation of any non-zero value is converted to true . If you're not programming in Visual Basic, you must convert your numeric string to a number before converting it to a Boolean. The following example illustrates this by converting an array of integers to Boolean values. Compare Boolean values Because Boolean values are either true or false , there is little reason to explicitly call the CompareTo method, which indicates whether an instance is greater than, less than, or equal to a specified value. Typically, to compare two Boolean variables, you call the Equals method or use your language's equality operator. However, when you want to compare a Boolean variable with the literal Boolean value true or false , it's not necessary to do an explicit comparison, because the result of evaluating a Boolean value is that Boolean value. For example, the following two expressions are equivalent, but the second is more compact. However, both techniques offer comparable performance. Work with Booleans as binary values A Boolean value occupies one byte of memory, as the following example shows. The C# example must be compiled with the /unsafe switch. The byte's low-order bit is used to represent its value. A value of 1 represents true ; a value of 0 represents false . You can use the System.Collections.Specialized.BitVector32 structure to work with sets of Boolean values. You can convert a Boolean value to its binary representation by calling the BitConverter.GetBytes(Boolean) method. The method returns a byte array with a single element. To restore a Boolean value from its binary representation, you can call the BitConverter.ToBoolean(Byte[], Int32) method. The following example calls the BitConverter.GetBytes method to convert a Boolean value to its binary representation and displays the individual bits of the value, and then calls the BitConverter.ToBoolean method to restore the value from its binary representation. Perform operations with Boolean values This section illustrates how Boolean values are used in apps. The first section discusses its use as a flag. The second illustrates its use for arithmetic operations. Boolean values as flags Boolean variables are most commonly used as flags, to signal the presence or absence of some condition. For example, in the String.Compare(String, String, Boolean) method, the final parameter, ignoreCase , is a flag that indicates whether the comparison of two strings is case-insensitive ( ignoreCase is true ) or case-sensitive ( ignoreCase is false ). The value of the flag can then be evaluated in a conditional statement. The following example uses a simple console app to illustrate the use of Boolean variables as flags. The app accepts command-line parameters that enable output to be redirected to a specified file (the /f switch), and that enable output to be sent both to a specified file and to the console (the /b switch). The app defines a flag named isRedirected to indicate whether output is to be sent to a file, and a flag named isBoth to indicate that output should be sent to the console. The F# example uses a recursive function to parse the arguments. Booleans and arithmetic operations A Boolean value is sometimes used to indicate the presence of a condition that triggers a mathematical calculation. For example, a hasShippingCharge variable might serve as a flag to indicate whether to add shipping charges to an invoice amount.
📥 下载地址(文章中间)
电脑助手解决一切电脑软件问题。
Because an operation with a false value has no effect on the result of an operation, it's not necessary to convert the Boolean to an integral value to use in the mathematical operation. Instead, you can use conditional logic. The following example computes an amount that consists of a subtotal, a shipping charge, and an optional service charge. The hasServiceCharge variable determines whether the service charge is applied. Instead of converting hasServiceCharge to a numeric value and multiplying it by the amount of the service charge, the example uses conditional logic to add the service charge amount if it is applicable. Booleans and interop While marshaling base data types to COM is generally straightforward, the Boolean data type is an exception. You can apply the MarshalAsAttribute attribute to marshal the Boolean type to any of the following representations: Represents the Boolean value false as a string. This field is read-only. Represents the Boolean value true as a string. This field is read-only. Compares this instance to a specified Boolean object and returns an integer that indicates their relationship to one another. Compares this instance to a specified object and returns an integer that indicates their relationship to one another. Returns a value indicating whether this instance is equal to a specified Boolean object. Returns a value indicating whether this instance is equal to a specified object. Returns the hash code for this instance. Returns the type code for the Boolean value type. Converts the specified span representation of a logical value to its Boolean equivalent. Converts the specified string representation of a logical value to its Boolean equivalent. Converts the value of this instance to its equivalent string representation (either "True" or "False"). Converts the value of this instance to its equivalent string representation (either "True" or "False"). Tries to format the value of the current boolean instance into the provided span of characters. Tries to convert the specified span representation of a logical value to its Boolean equivalent. Tries to convert the specified string representation of a logical value to its Boolean equivalent. Explicit Interface Implementations Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object. Returns the TypeCode for this instance. For a description of this member, see ToBoolean(IFormatProvider) . For a description of this member, see ToByte(IFormatProvider) . This conversion is not supported. Attempting to use this method throws an InvalidCastException . This conversion is not supported. Attempting to use this method throws an InvalidCastException . For a description of this member, see ToDecimal(IFormatProvider) . For a description of this member, see ToDouble(IFormatProvider) . For a description of this member, see ToInt16(IFormatProvider) . For a description of this member, see ToInt32(IFormatProvider) . For a description of this member, see ToInt64(IFormatProvider) . For a description of this member, see ToSByte(IFormatProvider) . For a description of this member, see ToSingle(IFormatProvider) . Converts the value of this instance to an equivalent string using the specified culture-specific formatting information. For a description of this member, see ToType(Type, IFormatProvider) . For a description of this member, see ToUInt16(IFormatProvider) . For a description of this member, see ToUInt32(IFormatProvider) . For a description of this member, see ToUInt64(IFormatProvider) . Parses a string into a value. Tries to parse a string into a value. Parses a span of characters into a value. Tries to parse a span of characters into a value. All members of this type are thread safe. Members that appear to modify instance state actually return a new instance initialized with the new value. As with any other type, reading and writing to a shared variable that contains an instance of this type must be protected by a lock to guarantee thread safety. Was this page helpful? Need help with this topic? Want to try using Ask Learn to clarify or guide you through this topic?
📥 下载地址(文章结尾)
电脑助手解决一切电脑软件问题。