|
104 | 104 |
|
105 | 105 | The following example illustrates formatting with the <xref:System.Boolean.ToString%2A> method. Note that the example uses the [composite formatting](/dotnet/standard/base-types/composite-formatting) feature, so the <xref:System.Boolean.ToString%2A> method is called implicitly.
|
106 | 106 |
|
107 |
| - [!code-csharp[System.Boolean.Structure#3](~/samples/snippets/csharp/VS_Snippets_CLR_System/system.boolean.structure/cs/tostring1.cs#3)] |
108 |
| - [!code-vb[System.Boolean.Structure#3](~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.boolean.structure/vb/tostring1.vb#3)] |
| 107 | + :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.boolean.structure/cs/tostring1.cs" interactive="try-dotnet" id="Snippet3"::: |
| 108 | + :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.boolean.structure/vb/tostring1.vb" id="Snippet3"::: |
109 | 109 |
|
110 | 110 | Because the <xref:System.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](/dotnet/csharp/language-reference/operators/conditional-operator) in C# or the [If operator](/dotnet/visual-basic/language-reference/operators/if-operator) in Visual Basic. The following example uses this technique to format <xref:System.Boolean> values as "Yes" and "No" rather than "True" and "False".
|
111 | 111 |
|
112 |
| - [!code-csharp[System.Boolean.Structure#4](~/samples/snippets/csharp/VS_Snippets_CLR_System/system.boolean.structure/cs/tostring2.cs#4)] |
113 |
| - [!code-vb[System.Boolean.Structure#4](~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.boolean.structure/vb/tostring2.vb#4)] |
| 112 | + :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.boolean.structure/cs/tostring2.cs" interactive="try-dotnet" id="Snippet4"::: |
| 113 | + :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.boolean.structure/vb/tostring2.vb" id="Snippet4"::: |
114 | 114 |
|
115 | 115 | For more complex custom formatting operations, including culture-sensitive formatting, you can call the <xref:System.String.Format%28System.IFormatProvider%2CSystem.String%2CSystem.Object%5B%5D%29?displayProperty=nameWithType> method and provide an <xref:System.ICustomFormatter> implementation. The following example implements the <xref:System.ICustomFormatter> and <xref:System.IFormatProvider> interfaces to provide culture-sensitive Boolean strings for the English (United States), French (France), and Russian (Russia) cultures.
|
116 | 116 |
|
117 |
| - [!code-csharp[System.Boolean.Structure#5](~/samples/snippets/csharp/VS_Snippets_CLR_System/system.boolean.structure/cs/format3.cs#5)] |
118 |
| - [!code-vb[System.Boolean.Structure#5](~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.boolean.structure/vb/format3.vb#5)] |
| 117 | + :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.boolean.structure/cs/format3.cs" interactive="try-dotnet" id="Snippet5"::: |
| 118 | + :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.boolean.structure/vb/format3.vb" id="Snippet5"::: |
119 | 119 |
|
120 | 120 | Optionally, you can use [resource files](/dotnet/framework/resources/) to define culture-specific Boolean strings.
|
121 | 121 |
|
|
129 | 129 |
|
130 | 130 | 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 <xref:System.Convert.ToBoolean%2A?displayProperty=nameWithType> class.
|
131 | 131 |
|
132 |
| - [!code-csharp[System.Boolean.Structure#6](~/samples/snippets/csharp/VS_Snippets_CLR_System/system.boolean.structure/cs/conversion1.cs#6)] |
133 |
| - [!code-vb[System.Boolean.Structure#6](~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.boolean.structure/vb/conversion1.vb#6)] |
| 132 | + :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.boolean.structure/cs/conversion1.cs" interactive="try-dotnet" id="Snippet6"::: |
| 133 | + :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.boolean.structure/vb/conversion1.vb" id="Snippet6"::: |
134 | 134 |
|
135 | 135 | When converting from floating-point values to Boolean values, the conversion methods perform an exact comparison with zero. If the floating-point value has lost precision, the result can be unexpected. This is illustrated in the following example, in which a <xref:System.Double> variable whose value should be zero is converted to a Boolean value. As the example shows, the result is `true` because repeated additions of 0.2 have resulted in a loss of precision.
|
136 | 136 |
|
137 | 137 | When converting from Boolean to numeric values, the conversion methods of the <xref:System.Convert> class convert `true` to 1 and `false` to 0. However, Visual Basic conversion functions convert `true` to either 255 (for conversions to <xref:System.Byte> values) or -1 (for all other numeric conversions). The following example converts `true` to numeric values by using a <xref:System.Convert> method, and, in the case of the Visual Basic example, by using the Visual Basic language's own conversion operator.
|
138 | 138 |
|
139 |
| - [!code-csharp[System.Boolean.Structure#8](~/samples/snippets/csharp/VS_Snippets_CLR_System/system.boolean.structure/cs/conversion3.cs#8)] |
140 |
| - [!code-vb[System.Boolean.Structure#8](~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.boolean.structure/vb/conversion3.vb#8)] |
| 139 | + :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.boolean.structure/cs/conversion3.cs" interactive="try-dotnet" id="Snippet8"::: |
| 140 | + :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.boolean.structure/vb/conversion3.vb" id="Snippet8"::: |
141 | 141 |
|
142 | 142 | For conversions from <xref:System.Boolean> to string values, see the [Formatting Boolean Values](#Formatting) section. For conversions from strings to <xref:System.Boolean> values, see the [Parsing Boolean Values](#Parsing) section.
|
143 | 143 |
|
|
147 | 147 |
|
148 | 148 | The following example uses the <xref:System.Boolean.Parse%2A> and <xref:System.Boolean.TryParse%2A> methods to parse a number of strings. Note that only the case-insensitive equivalents of "True" and "False" can be successfully parsed.
|
149 | 149 |
|
150 |
| - [!code-csharp[System.Boolean.Structure#2](~/samples/snippets/csharp/VS_Snippets_CLR_System/system.boolean.structure/cs/parse2.cs#2)] |
151 |
| - [!code-vb[System.Boolean.Structure#2](~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.boolean.structure/vb/parse2.vb#2)] |
| 150 | + :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.boolean.structure/cs/parse2.cs" interactive="try-dotnet" id="Snippet2"::: |
| 151 | + :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.boolean.structure/vb/parse2.vb" id="Snippet2"::: |
152 | 152 |
|
153 | 153 | If you are 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 are 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.
|
154 | 154 |
|
155 |
| - [!code-csharp[System.Boolean.Structure#9](~/samples/snippets/csharp/VS_Snippets_CLR_System/system.boolean.structure/cs/parse3.cs#9)] |
156 |
| - [!code-vb[System.Boolean.Structure#9](~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.boolean.structure/vb/parse3.vb#9)] |
| 155 | + :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.boolean.structure/cs/parse3.cs" interactive="try-dotnet" id="Snippet9"::: |
| 156 | + :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.boolean.structure/vb/parse3.vb" id="Snippet9"::: |
157 | 157 |
|
158 | 158 | <a name="Comparing"></a>
|
159 | 159 | ## Comparing Boolean values
|
|
186 | 186 |
|
187 | 187 | The following example calls the <xref:System.BitConverter.GetBytes%2A?displayProperty=nameWithType> method to convert a Boolean value to its binary representation and displays the individual bits of the value, and then calls the <xref:System.BitConverter.ToBoolean%2A?displayProperty=nameWithType> method to restore the value from its binary representation.
|
188 | 188 |
|
189 |
| - [!code-csharp[System.Boolean.Structure#1](~/samples/snippets/csharp/VS_Snippets_CLR_System/system.boolean.structure/cs/binary1.cs#1)] |
190 |
| - [!code-vb[System.Boolean.Structure#1](~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.boolean.structure/vb/binary1.vb#1)] |
| 189 | + :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.boolean.structure/cs/binary1.cs" interactive="try-dotnet" id="Snippet1"::: |
| 190 | + :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.boolean.structure/vb/binary1.vb" id="Snippet1"::: |
191 | 191 |
|
192 | 192 | <a name="Operations"></a>
|
193 | 193 | ## Performing operations with Boolean values
|
|
198 | 198 |
|
199 | 199 | 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.
|
200 | 200 |
|
201 |
| - [!code-csharp[System.Boolean.Structure#10](~/samples/snippets/csharp/VS_Snippets_CLR_System/system.boolean.structure/cs/operations1.cs#10)] |
202 |
| - [!code-vb[System.Boolean.Structure#10](~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.boolean.structure/vb/operations1.vb#10)] |
| 201 | + :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.boolean.structure/cs/operations1.cs" id="Snippet10"::: |
| 202 | + :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.boolean.structure/vb/operations1.vb" id="Snippet10"::: |
203 | 203 |
|
204 | 204 | ### Booleans and arithmetic operations
|
205 | 205 | 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.
|
|
0 commit comments