C# Number Examples

Array Collections File String Windows VB.NET Algorithm ASP.NET Cast Class Compression Convert Data Delegate Directive Enum Exception If Interface Keyword LINQ Loop Method .NET Number Regex Sort StringBuilder Struct Switch Time Value

Division illustration

In the early 1970s the first microprocessor was introduced. Numeric computations became more efficient and reliable than ever before. Programs today constantly use numbers. All programs—even C# programs—are computational beings that think only in numbers.

Bit Tips Math Class Random Number

If you must write your own random number generator, be sure to test it carefully.
Stroustrup, p. 685

Types

This program introduces some numeric types in the C# programming language. It declares and initializes one byte, short, int, and long variable to their maximum values, and then prints those values to the screen.

Program that uses numbers [C#]

using System;

class Program
{
    static void Main()
    {
	byte a = byte.MaxValue;
	short b = short.MaxValue;
	int c = int.MaxValue;
	long d = long.MaxValue;

	Console.WriteLine(a);
	Console.WriteLine(b);
	Console.WriteLine(c);
	Console.WriteLine(d);
    }
}

Output

255
32767
2147483647
9223372036854775807

Overview: This C# section explains numbers and numeric types. It has articles on operators and random numbers.

Examples

Detailed information on numeric types. We reveal how you can use numeric types in your C# programs. They also show the maximum values, minimum values, and also memory usage for the types.

byte decimal float long sbyte short uint ulong ushort

Using int type. The most famous numeric type in the world is probably the int type. This is used in most every program in constructs such as the for-loop. Int types are sized for optimal performance as local variables.

int int.MaxValue Int16, Int32, and Int64 TypesDouble keyword

Using double type. The double type is another low-level type and it is twice the size in bytes than the int type. We talk about this type and ways you can employ it.

double Single and Double Types

Operators

Operators are typically written in symbolic notation in programs. They include the +, -, *, and / operators for arithmetic operations. These are called binary operators, which refers to the fact they act upon two operands—we demonstrate operators.

Operator keyword Decrement Int Divide by Powers of Two Divide Numbers Increment Int Modulo Operator Multiply Numbers Preincrement Integers

Syntax tips

Programming tip

Numeric types in the C# language use a slightly different syntax than object types. We show how you can cast and use suffixes on numeric types and constants.

Numeric Casts Numeric Promotion Suffix Examples, Numeric Suffixes

Random

Question and answer

There are many different ways to generate random numbers in the C# language, but the easiest methods involve the Random type. This short program shows how you can use the Random type to generate random numbers. You must first instantiate the Random instance and then you can call the Next method repeatedly to get new random numbers.

Random Number
Program that uses Random type [C#]

using System;

class Program
{
    static void Main()
    {
	Random random = new Random();
	Console.WriteLine(random.Next());
	Console.WriteLine(random.Next());
    }
}

Output

1592498984
1526415661

Computations

Percentage symbol

We are not in math class any more, but we still need to compute percentages and determine prime numbers. Fortunately, these examples dip into these topics and show us how to do this without actually thinking very hard.

Percentage Prime Number Multiplication Table

Even and odd. It is quite easy to see if numbers are even or odd. This requires using the modulo division operator and then checking the result against zero.

Even Numbers Odd Numbers

Word representations

In the English language, you are supposed to write out the word "one" instead of "1" in formal text. You can perform this sort of substitution in the C# language using an array or lookup table, as we see here.

Number Words

Performance

Performance optimization

We show a way you can marginally improve performance of certain arithmetic expressions. Instead of using local variables, you can write a compound statement using parentheses.

Arithmetic Expression Optimization

Summary

The C# programming language

We looked into numbers and handling numeric types in the C# programming language. Numeric types are core to the C# language. An understanding of how to use them is important. And like many topics in computer programming, they are more complicated than they first appear.

Dot Net Perls