In C + +, a value of type bool can be converted to a value of type int, and false is equivalent to a value of 0, and true is equivalent to a value other than 0. However, in the C # language, there is no reciprocal conversion between the bool type and other types. For example, the following if statement is illegal in C # and is legal in C + +:
int x = 123;
if (x) // 注意:在C#中此语句是错误的
{
printf("x is 非零值.");
}
To test a variable of type int, you must explicitly compare the variable with a value, such as 0, as follows:
int x = 123;
if (x != 0) // C#的判断方式
{
Console.Write("The value of x is nonzero.");
}
Function Description: Enter a character from the keyboard, and then the program checks whether the entered character is a letter. If the character entered is a letter, the program check is uppercase or lowercase. These checks are performed using Isletter and Islower (both of which return the bool type).
using System;
public class BoolTest1
{
static void Main()
{
Console.Write("请输入一个字母: ");
char c = (char)Console.Read();
if (Char.IsLower(c))
{
Console.WriteLine("这个字符是小写字母.");
}
else
{
Console.WriteLine("这个字符是大写字母.");
}
}
}
There are predefined implicit conversions from byte to short, ushort, int, uint, long, ulong, float, double, or decimal.
Attention
You cannot implicitly convert a non-literal numeric type of a larger storage range to byte.
such as: byte z = x + y;
The above assignment statement will result in a compilation error because the arithmetic expression on the right side of the assignment operator evaluates to the int type by default.
To resolve this issue, use the cast:
byte z = (byte) (x + y);