Home  >  Article  >  Backend Development  >  C# Learning Diary 05---Data Type of Boolean Type

C# Learning Diary 05---Data Type of Boolean Type

黄舟
黄舟Original
2017-01-20 13:27:201551browse

Boolean type of value type:

The Boolean type is used to represent the two concepts of 'true' and 'false'. Although it looks very simple, it is actually used very widely. We know that computers It uses binary to represent various data, and there are only 0 or 1 inside it. The logical variables represented by the Boolean type are only 2, True or False.

Here we compare C/C++, Boolean type The meaning in C/C++ is the same as that in C#. It represents the two values ​​​​of 'true' or 'false'. 0 represents 'false' and other non-0 numbers represent 'true'. This informal expression is used in C#. Abandoned. In C#, True and False cannot be replaced by other values, and there is no conversion between integer types and Boolean types. That is to say, it is illegal to convert integer types into Boolean types.

bool x=1 //Error

bool x=true or x = false //Correct

Next, define a Boolean variable and output:

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace Example  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            bool b=true;  
          Console.WriteLine(b);  
  
        }  
    }  
}

The result is:

     True

The above is the content of C# Learning Diary 05---Boolean type of data type. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn