Csharp/C Sharp/Data Types/bool
Содержание
A static method that returns a Boolean value.
using System;
class MainClass{
public static Boolean TestInput(int val) {
if (val < 1 || val > 10)
return false;
return true;
}
public static void Main() {
Console.WriteLine("TestInput(0) = {0}", TestInput(0));
Console.WriteLine("TestInput(1) = {0}", TestInput(1));
Console.WriteLine("TestInput(5) = {0}", TestInput(5));
Console.WriteLine("TestInput(11) = {0}", TestInput(11));
}
}
bool FalseString, TrueString
using System;
using System.Collections.Generic;
using System.Text;
class Program {
static void Main(string[] args) {
bool b3 = true; // No problem.
bool b4 = false; // No problem.
Console.WriteLine("-> bool.FalseString: {0}", bool.FalseString);
Console.WriteLine("-> bool.TrueString: {0}", bool.TrueString);
}
}
bool variable
using System;
class Operators {
static void Main() {
bool a = 4 > 5;
Console.WriteLine("{0}", a);
}
}
Demonstrate bool values
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate bool values.
using System;
public class BoolDemo {
public static void Main() {
bool b;
b = false;
Console.WriteLine("b is " + b);
b = true;
Console.WriteLine("b is " + b);
// a bool value can control the if statement
if(b) Console.WriteLine("This is executed.");
b = false;
if(b) Console.WriteLine("This is not executed.");
// outcome of a relational operator is a bool value
Console.WriteLine("10 > 9 is " + (10 > 9));
}
}
Print a truth table for the logical operators
/*
C# A Beginner"s Guide
By Schildt
Publisher: Osborne McGraw-Hill
ISBN: 0072133295
*/
/*
Project 2-2
Print a truth table for the logical operators.
*/
using System;
public class LogicalOpTable {
public static void Main() {
bool p, q;
Console.WriteLine("P\tQ\tAND\tOR\tXOR\tNOT");
p = true; q = true;
Console.Write(p + "\t" + q +"\t");
Console.Write((p&q) + "\t" + (p|q) + "\t");
Console.WriteLine((p^q) + "\t" + (!p));
p = true; q = false;
Console.Write(p + "\t" + q +"\t");
Console.Write((p&q) + "\t" + (p|q) + "\t");
Console.WriteLine((p^q) + "\t" + (!p));
p = false; q = true;
Console.Write(p + "\t" + q +"\t");
Console.Write((p&q) + "\t" + (p|q) + "\t");
Console.WriteLine((p^q) + "\t" + (!p));
p = false; q = false;
Console.Write(p + "\t" + q +"\t");
Console.Write((p&q) + "\t" + (p|q) + "\t");
Console.WriteLine((p^q) + "\t" + (!p));
}
}
Using Bool
/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
* Version: 1
*/
using System;
namespace Client.Chapter_1___Common_Type_System
{
public class UsingBoolChapter_1___Common_Type_System {
static void Main(string[] args)
{
bool MyBool = false;
if (MyBool)
{
Console.WriteLine(MyBool);
}
else
{
Console.WriteLine(MyBool);
}
}
}
}