Types of data in C #

Source: Internet
Author: User

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);

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.