C# Math: Operators and Functions for Fun and Profit

Now that we have some basic types and can create variables, it would sure be nice to have them interact with each other. Operators can be used to interact with our variables.

Download this lesson as a Polyglot Notebook to open in Visual Studio Code, or open directly in your web browser with Binder.

Let's start by declaring two variables, playerOneHand and playerTwoHand and interact with them using different operators. Try changing some of the values and tinkering with the operators in the following code snippets.

var playerOneHand = 5; // 5 cards in player 1's hand
var playerTwoHand = 4;

Basic arithmetic operators + - * / are available:

var totalCards = playerOneHand + playerTwoHand;
var whoHasMoreCards = playerOneHand - playerTwoHand;
var takeAnotherCard = playerTwoHand + 1;
var cardsPerSuit = 52 / 4;
var lotsOfCards = 52 * 10;

The remainder or modulo operator % is also available when working with floating point numbers:

var angleCardIsTurned = 45.2m;
var fractionalPart = angleCardIsTurned % 1;  // returns 0.2

Incrementing values

Numeric values can be incremented by using the ++ operator. This will increment the value of the variable after the operation concludes. It's typically used like this:

// Draw a card
var cardsInHand = 3;
cardsInHand++;

Additionally, the ++ operator can also be pre-pended to the variable they will effect but with a slightly different behavior. In this scenario, the value is incremented BEFORE the operation concludes. Let's use the display() method in Polyglot notebooks to show these values:

var cardsInHand = 3;

display(cardsInHand++);   // Display the value then increment to 4, output is 3
display(cardsInHand);     // output is 4
display(++cardsInHand);   // output is 5
display(cardsInHand);     // output is 5

There is also a decrementing operator available, -- that can be used in the same way to decrement a value by decorating a variable with -- as a prefix or suffix.

var cardsInHand = 5;

// play a card
cardsInHand--;

You can use the equals symbol = with the four standard arithmetic operators to calculate with the current value and assign a value back to a variable. These are called the compound assignment operators.

var playerOneHand = 5;
var playerTwoHand = 4;

// Take another card player two
playerTwoHand += 1;
// playerTwoHand now is the value 5

// Play a card player one
playerOneHand -=1
// playerOneHand now is the value 4

Equality and Inequality testing

C# makes the inequality operators available as well, and a test for equality using ==. This expression resolves to a boolean true or false value.


playerOneHand < playerTwoHand

playerTwoHand > playerOneHand

playerOneHand >= playerTwoHand

playerOneHand <= playerTwoHand

playerOneHand == playerTwoHand

The Standard .NET Math library

There are a ton of math functions available for you on the System.Math object available in all C# programs. Between trigonometric functions and rounding simple statistic functions and even logarithmic, exponents and square roots you'll have a great collection of functions available to you once you start working with System.Math

Try some of these operations on our two player's hands of cards:

var smallerHand = Math.Min(playerOneHand, playerTwoHand);

var largerHand = Math.Max(playerOneHand, playerTwoHand);

var angleCardTurned = 45.25m;
var estimatedAngle = Math.Round(angleCardTurned, 1);

var closeEnoughAngle = Math.Truncate(angleCardTurned);

About Us

CSharp In the Cards is a free technical educational series produced by Jeffrey T. Fritz and available in video, text, and source code formats. In the future, we plan to have more formats available

Contact Us

An unhandled error has occurred. Reload 🗙