Everything in C# is an object

As C# is an object oriented language, everything we want to work with is an object. Objects can be declared of various TYPES and then interacted with.

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

Let's consider our deck of cards example. We can model all different states and information about the deck of cards using variables, and we'll use that as a sample scenario throughout this module.

The simplest types in C# are called Built-In Types and have short keywords available that we can use to declare the type of a variable:

Keyword Type Name Description Valid Values
bool System.Boolean Boolean value that can be true or false true, false
byte System.Byte Unsigned 8-bit integer 0 to 255
sbyte System.SByte Signed 8-bit integer -127 to 127
char System.Char Single character storage in UTF-16 format Valid UTF-16 characters
decimal System.Decimal Floating point number with 28-29 digits of precision in 16 bytes ?1.0 x 10-28 to7.9228 x 1028
double System.Double Floating point number with 15-17 digits of precision in 8 bytes ?5.0 10?324 to1.7 10308
float System.Single Floating point number with 6-9 digits of precision in 4 bytes ?1.5 x 10?45 to3.4 x 1038
int System.Int32 Signed 32-bit integer -2,147,483,648 to 2,147,483,647
uint System.UInt32 Unsigned 32-bit integer 0 to 4,294,967,295
nint System.IntPtr Platform dependent integer Signed 32-bit or 64-bit integer
nuint System.UIntPtr Platform dependent unsigned integer Unsigned 32-bit or 64-bit integer
long System.Int64 Signed 64-bit integer -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
ulong System.UInt64 Unsigned 64-bit integer 0 to 18,446,744,073,709,551,615
short System.Int16 Signed 16-bit integer -32,768 to 32,767
ushort System.UInt16 Unsigned 16-bit integer 0 to 65,535

We can define variables, in-memory storage for a type, by preceding the name of the variable we would like to create with the type of the variable we are creating.

int countOfCards;

That's not very exciting, however we created a 32-bit integer named countOfCards that we could use elsewhere in our code.We can initialize a variable with an = assignment at the time of declaration.

int countOfCards = 52;   // 52 cards in the deck
char suit = 'H';         // The suit of a card is identified by a single character, H for hearts
bool faceDown = true;    // A card is face down

Large numbers can get difficult for us to read when we assign them like this. Fortunately, C# allows us to use an underscore character _ to separate digits in ANY grouping that you'd like and the compiler will simply ignore the character.

int oneMillionCards = 1_000_000;
int anotherMillion = 1000_000;
int aMillionAgain = 100000_0;

Perhaps you would like to assign numbers using hexadecimal or binary values. Those are supported with the 0x prefix for hexadecimal and 0b or 0B prefix for binary.

int thisYear = 2024;
int thisYearInHex = 0x7E8;
int thisYearInBinary = 0b_0111_1110_1000;

These variables can be changed elsewhere in our application. If we wanted to change our cards to be face up, we could re-assign the faceDown variable like this:

faceDown = false;

Notice that we are not declaring the type, that only needs to be done once, and we are just changing the value assigned to the variable.

The var keyword

Sometimes, its a little cumbersome to declare a variable, assign a value, and have to specify the type before it.C# has built-in type inference and you can use the var keyword to force the compiler to detect the actual type being createdand set the variable to the type of the value being assigned.

var countOfCards = 52;
var suit = 'H';
var faceDown = true;

You can ONLY use the var keyword when creating and assigning the variable in one statement.

Real Literals

When you declare a numeric type, by default it is allocated as an int or a 32-bit integer type. We can instead declare double, float, and decimal types with simple numeric notation, but we need to force the literal numbers we assign to be the correct type to match the variable type expected.

To do this, we add a d, f, or m suffix to a number being assigned.

var angleCardIsTurned = 45.0m;
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 🗙