C# True False
- "true" and "false" are the only two boolean literal values in C#.
- You cannot use True or FALSE as the boolean literal becauase C# is case sensitive language.
- "true" or "false" can be assigned to any boolean variables.
- The boolean data type in C# is bool not boolean or Boolean.
Example 01-17-01:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System; namespace TestTrueFalse { class Program { static void Main(string[] args) { bool isFolderExist = true; bool isFileExist = true; //isFolderExist = TRUE; //isFileExist = False; Console.WriteLine(isFolderExist && isFileExist ? "Existed" : "Not existed"); isFileExist = false; Console.WriteLine(isFolderExist && isFileExist ? "Existed" : "Not existed"); Console.Read(); } } }
Output
Existed Not existed
- Line 9-10: Define 2 boolean variables and assign true literal to them.
- Line 15: Output the result. ?: operator is used to output the string based on the condition. Because they are all true, "Existed" is printed.
- Line 17: This time literal false is assigned to isFileExist.
- Line 18: Output "Not existed" because the condition is false.
- Line 12-13: These 2 lines were commented out. If either of double slash were removed, you would get a compile-time error. Because either TRUE or False is not a boolean literal.