TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Bool Methods, Return True and False

This C# example program shows methods and properties that return bools.

Bool method. A method returns true or false.

In this way, it can enhance your code with greater abstraction. Returning true and false from a method is a way to improve the object-orientation of your application. It simplifies it.

True, False

Example. Here we declare a class that has several fields, which determine its state. When you have complex classes, you can expose public methods (or properties) that return calculated bool values.

Return

Often: Developers will use the "Is" prefix, to indicate the type of result. This is a convention.

Tip: It is usually clearest to phrase all boolean properties and methods in the positive, and then test them for false if required.

Based on:

.NET 4.5

C# program that returns true and false from method

using System;

class Employee
{
    bool _fired = false;
    bool _hired = true;
    int _salary = 10000;

    public bool IsCurrent()
    {
	return !this._fired &&
	    this._hired &&
	    this._salary > 0;
    }

    public bool IsExecutive()
    {
	return IsCurrent() &&
	    this._salary > 1000000;
    }

    public bool Fired
    {
	get
	{
	    return this._fired;
	}
    }
}

class Program
{
    static void Main()
    {
	Employee employee = new Employee();
	if (employee.IsCurrent())
	{
	    Console.WriteLine("Is currently employed");
	}
	if (employee.IsExecutive())
	{
	    Console.WriteLine("Is an executive");
	}
	if (!employee.Fired)
	{
	    Console.WriteLine("Is not fired yet");
	}
    }
}

Output

Is currently employed
Is not fired yet

The Employee class contains three member variables, including two bools and an integer. The methods IsCurrent(), IsEmployee() and the property Fired provide an abstraction of the internal state of the object.

Tip: When you return boolean values, you can chain expressions with logical "AND" and "OR".

The Fired property returns the value of the internal state member with the same name. In C# programs, using properties allows more flexibility when changing the implementation of classes without affecting other parts of your program.

Property

Discussion. The bool type is a common type to use as the return type in methods in C# programs. The class above provides the syntax for this pattern. Next, we look at the logic regarding boolean result values and methods that return bools.

Understanding complicated boolean tests in detail is rarely necessary for understanding program flow. Putting such a test into a function makes the code more readable because 1—the details of the test are out of the way and 2—a descriptive function name summarizes the purpose of the test.

Code Complete

Summary. We developed a class with methods and properties returning bools. We discussed the principles of abstraction in object-oriented programming, and researched the topic in one of the leading books about software construction.

And: We chained these conditional expressions, with short-circuit expressions, for the clearest code.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf