当前位置: 首页>>代码示例>>C#>>正文


C# Boolean结构体代码示例

本文整理汇总了C#中System.Boolean结构体的典型用法代码示例。如果您正苦于以下问题:C# Boolean结构体的具体用法?C# Boolean怎么用?C# Boolean使用的例子?那么恭喜您, 这里精选的结构体代码示例或许可以为您提供帮助。


Boolean结构体属于System命名空间,在下文中一共展示了Boolean结构体的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      bool raining = false;
      bool busLate = true;

      Console.WriteLine("It is raining: {0}", raining);
      Console.WriteLine("The bus is late: {0}", busLate);
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:14,代码来源:Boolean

输出:

It is raining: False
The bus is late: True

示例2: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      bool raining = false;
      bool busLate = true;

      Console.WriteLine("It is raining: {0}", 
                        raining ? "Yes" : "No");
      Console.WriteLine("The bus is late: {0}", 
                        busLate ? "Yes" : "No" );
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:16,代码来源:Boolean

输出:

It is raining: No
The bus is late: Yes

示例3: Main

//引入命名空间
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      String[] cultureNames = { "", "en-US", "fr-FR", "ru-RU" };
      foreach (var cultureName in cultureNames) {
         bool value = true;
         CultureInfo culture = CultureInfo.CreateSpecificCulture(cultureName);
         BooleanFormatter formatter = new BooleanFormatter(culture);
         
         String result = String.Format(formatter, "Value for '{0}': {1}", culture.Name, value);
         Console.WriteLine(result);
      }
   }
}

public class BooleanFormatter : ICustomFormatter, IFormatProvider
{   
   private CultureInfo culture;
   
   public BooleanFormatter() : this(CultureInfo.CurrentCulture)
   { }
   
   public BooleanFormatter(CultureInfo culture)
   {
      this.culture = culture; 
   }
   
   public Object GetFormat(Type formatType)
   { 
      if (formatType == typeof(ICustomFormatter))
         return this;
      else
         return null;
   }
   
   public String Format(String fmt, Object arg, IFormatProvider formatProvider)
   { 
      // Exit if another format provider is used.
      if (! formatProvider.Equals(this)) return null;
      
      // Exit if the type to be formatted is not a Boolean
      if (! (arg is Boolean)) return null;
      
      bool value = (bool) arg;
      switch (culture.Name) {
         case "en-US":
            return value.ToString();
         case "fr-FR":
            if (value) 
               return "vrai";
            else
               return "faux";
         case "ru-RU":
            if (value)
               return "верно";
            else
               return "неверно";
         default:
            return value.ToString();  
      }
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:67,代码来源:Boolean

输出:

Value for '': True
Value for 'en-US': True
Value for 'fr-FR': vrai
Value for 'ru-RU': верно

示例4: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      Byte byteValue = 12;
      Console.WriteLine(Convert.ToBoolean(byteValue));
      Byte byteValue2 = 0;
      Console.WriteLine(Convert.ToBoolean(byteValue2));
      int intValue = -16345;
      Console.WriteLine(Convert.ToBoolean(intValue));
      long longValue = 945;
      Console.WriteLine(Convert.ToBoolean(longValue));
      SByte sbyteValue = -12;
      Console.WriteLine(Convert.ToBoolean(sbyteValue));
      double dblValue = 0;
      Console.WriteLine(Convert.ToBoolean(dblValue));
      float sngValue = .0001f;
      Console.WriteLine(Convert.ToBoolean(sngValue));
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:23,代码来源:Boolean

输出:

True
False
True
True
True
False
True

示例5: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      bool flag = true;
      
      byte byteValue;   
      byteValue = Convert.ToByte(flag);
      Console.WriteLine("{0} -> {1}", flag, byteValue);         
      
      sbyte sbyteValue;
      sbyteValue = Convert.ToSByte(flag);
      Console.WriteLine("{0} -> {1}", flag, sbyteValue);         

      double dblValue;
      dblValue = Convert.ToDouble(flag);
      Console.WriteLine("{0} -> {1}", flag, dblValue);         

      int intValue;
      intValue = Convert.ToInt32(flag);
      Console.WriteLine("{0} -> {1}", flag, intValue);         
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:26,代码来源:Boolean

输出:

True -> 1
True -> 1
True -> 1
True -> 1

示例6: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      string[] values = { null, String.Empty, "True", "False", 
                          "true", "false", "    true    ", 
                           "TrUe", "fAlSe", "fa lse", "0",  
                          "1", "-1", "string" };
      // Parse strings using the Boolean.Parse method.                    
      foreach (var value in values) {
         try {
            bool flag = Boolean.Parse(value);
            Console.WriteLine("'{0}' --> {1}", value, flag);
         }
         catch (ArgumentException) {
            Console.WriteLine("Cannot parse a null string.");
         }   
         catch (FormatException) {
            Console.WriteLine("Cannot parse '{0}'.", value);
         }         
      }
      Console.WriteLine();
      // Parse strings using the Boolean.TryParse method.                    
      foreach (var value in values) {
         bool flag = false;
         if (Boolean.TryParse(value, out flag))
            Console.WriteLine("'{0}' --> {1}", value, flag);
         else
            Console.WriteLine("Unable to parse '{0}'", value);
      }                                     
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:35,代码来源:Boolean

输出:

Cannot parse a null string.
Cannot parse ''.
'True' --> True
'False' --> False
'true' --> True
'false' --> False
'    true    ' --> True
'TrUe' --> True
'fAlSe' --> False
Cannot parse 'fa lse'.
Cannot parse '0'.
Cannot parse '1'.
Cannot parse '-1'.
Cannot parse 'string'.

Unable to parse ''
Unable to parse ''
'True' --> True
'False' --> False
'true' --> True
'false' --> False
'    true    ' --> True
'TrUe' --> True
'fAlSe' --> False
Cannot parse 'fa lse'.
Unable to parse '0'
Unable to parse '1'
Unable to parse '-1'
Unable to parse 'string'

示例7: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      String[] values = { "09", "12.6", "0", "-13 " };
      foreach (var value in values) {
         bool success, result;
         int number; 
         success = Int32.TryParse(value, out number);
         if (success) {
            // The method throws no exceptions.
            result = Convert.ToBoolean(number);
            Console.WriteLine("Converted '{0}' to {1}", value, result);
         }
         else {
            Console.WriteLine("Unable to convert '{0}'", value); 
         }         
      }
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:23,代码来源:Boolean

输出:

Converted '09' to True
Unable to convert '12.6'
Converted '0' to False
Converted '-13 ' to True

示例8: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      bool[] hasServiceCharges = { true, false };
      Decimal subtotal = 120.62m;
      Decimal shippingCharge = 2.50m;
      Decimal serviceCharge = 5.00m;
      
      foreach (var hasServiceCharge in hasServiceCharges) {
         Decimal total = subtotal + shippingCharge + 
                                (hasServiceCharge ? serviceCharge : 0);
         Console.WriteLine("hasServiceCharge = {1}: The total is {0:C2}.", 
                           total, hasServiceCharge);                       
      }
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:20,代码来源:Boolean

输出:

hasServiceCharge = True: The total is $128.12.
hasServiceCharge = False: The total is $123.12.

示例9: Main

//引入命名空间
using System;

public struct BoolStruct
{
   public bool flag1;
   public bool flag2;
   public bool flag3;
   public bool flag4;
   public bool flag5;
}

public class Example
{
   public static void Main()
   {
      unsafe {
         BoolStruct b = new BoolStruct();
         bool* addr = (bool*) &b;
         Console.WriteLine("Size of BoolStruct: {0}", sizeof(BoolStruct));
         Console.WriteLine("Field offsets:");
         Console.WriteLine("   flag1: {0}", (bool*) &b.flag1 - addr);
         Console.WriteLine("   flag1: {0}", (bool*) &b.flag2 - addr);
         Console.WriteLine("   flag1: {0}", (bool*) &b.flag3 - addr);
         Console.WriteLine("   flag1: {0}", (bool*) &b.flag4 - addr);
         Console.WriteLine("   flag1: {0}", (bool*) &b.flag5 - addr);
      }
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:29,代码来源:Boolean

输出:

Size of BoolStruct: 5
Field offsets:
flag1: 0
flag1: 1
flag1: 2
flag1: 3
flag1: 4

示例10: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      bool[] flags = { true, false };
      foreach (var flag in flags) {
         // Get binary representation of flag.
         Byte value = BitConverter.GetBytes(flag)[0];
         Console.WriteLine("Original value: {0}", flag);
         Console.WriteLine("Binary value:   {0} ({1})", value, 
                           GetBinaryString(value));
         // Restore the flag from its binary representation.
         bool newFlag = BitConverter.ToBoolean( new Byte[] { value }, 0);
         Console.WriteLine("Restored value: {0}\n", flag);
      }
   }
   
   private static string GetBinaryString(Byte value)
   {
      String retVal = Convert.ToString(value, 2);
      return new String('0', 8 - retVal.Length) + retVal;
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:26,代码来源:Boolean

输出:

Original value: True
Binary value:   1 (00000001)
Restored value: True

Original value: False
Binary value:   0 (00000000)
Restored value: False

示例11: Main

//引入命名空间
using System;
using System.IO;
using System.Threading;

public class Example
{
   public static void Main()
   {
      // Initialize flag variables.
      bool isRedirected = false;
      bool isBoth = false; 
      String fileName = "";
      StreamWriter sw = null;
      
      // Get any command line arguments.
      String[] args = Environment.GetCommandLineArgs();
      // Handle any arguments.
      if (args.Length > 1) { 
         for (int ctr = 1; ctr < args.Length; ctr++) {
            String arg = args[ctr];
            if (arg.StartsWith("/") || arg.StartsWith("-")) {
               switch (arg.Substring(1).ToLower())
               {
                  case "f":
                     isRedirected = true;
                     if (args.Length < ctr + 2) {
                        ShowSyntax("The /f switch must be followed by a filename.");
                        return;
                     }
                     fileName = args[ctr + 1];
                     ctr++;
                     break;
                  case "b":
                     isBoth = true;
                     break;
                  default:
                     ShowSyntax(String.Format("The {0} switch is not supported", 
                                              args[ctr]));
                     return;
               }
            }   
         }
      }

      // If isBoth is True, isRedirected must be True.
      if (isBoth &&  ! isRedirected) { 
         ShowSyntax("The /f switch must be used if /b is used.");
         return;
      }

      // Handle output.
      if (isRedirected) {
         sw = new StreamWriter(fileName); 
         if (!isBoth)
            Console.SetOut(sw); 
      }     
      String msg = String.Format("Application began at {0}", DateTime.Now);
      Console.WriteLine(msg);
      if (isBoth) sw.WriteLine(msg);
      Thread.Sleep(5000);
      msg = String.Format("Application ended normally at {0}", DateTime.Now);
      Console.WriteLine(msg);
      if (isBoth) sw.WriteLine(msg);
      if (isRedirected) sw.Close();
   }
   
   private static void ShowSyntax(String errMsg)
   {
      Console.WriteLine(errMsg);
      Console.WriteLine("\nSyntax: Example [[/f <filename> [/b]]\n");
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:73,代码来源:Boolean


注:本文中的System.Boolean结构体示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。