/* * Author: Kishore Reddy * Url: http://commonlibrarynet.codeplex.com/ * Title: CommonLibrary.NET * Copyright: ? 2009 Kishore Reddy * License: LGPL License * LicenseUrl: http://commonlibrarynet.codeplex.com/license * Description: A C# based .NET 3.5 Open-Source collection of reusable components. * Usage: Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using System.Collections.Generic; using System.Text; namespace GenericCode { public class StringHelpers { /// <summary> /// Convert boolean value to "Yes" or "No" /// </summary> /// <param name="b"></param> /// <returns></returns> public static string ConvertBoolToYesNo(bool b) { if (b) { return "Yes"; } return "No"; } } }
Data Types
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); } }
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 Q AND OR XOR NOT"); p = true; q = true; Console.Write(p + " " + q +" "); Console.Write((p&q) + " " + (p|q) + " "); Console.WriteLine((p^q) + " " + (!p)); p = true; q = false; Console.Write(p + " " + q +" "); Console.Write((p&q) + " " + (p|q) + " "); Console.WriteLine((p^q) + " " + (!p)); p = false; q = true; Console.Write(p + " " + q +" "); Console.Write((p&q) + " " + (p|q) + " "); Console.WriteLine((p^q) + " " + (!p)); p = false; q = false; Console.Write(p + " " + q +" "); Console.Write((p&q) + " " + (p|q) + " "); Console.WriteLine((p^q) + " " + (!p)); } }
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)); } }
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); } } } }
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 variable
using System; class Operators { static void Main() { bool a = 4 > 5; Console.WriteLine("{0}", a); } }