C# Data Types Explained

A fundamental element to programming languages, including C#, is data types. Without data types, compilers and interpreters have no way of knowing how a programmer intends to use the data they are inputting, much less what type of data has been entered. This is a beginner level programming tutorial aimed at developers that are looking to learn the C# programming language. Is it intended for either new coders or veteran programmers looking to add C# as a second (or third) language to increase their developer toolset.

What is a Data Type in C#?

Data types describe the type of information – or data – that a developer is inputting into a computer. These data types, in turn, get fed to the compiler or interpreter so that they can better understand how a programmer intends to use the data. Imagine, for a moment, that someone is speaking to you in gibberish. You have never heard the language they are speaking in and you have no reference point to tell if they are telling you letters, numbers, or words, much less what type of numbers – if any. Further complicate that scenario by imagining that you cannot even tell if they are using the most basic form of communication: yes or no questions and answers.

Working backwards, if you did know, at the very least, whether they were trying to tell you a word, a number, or a simple yes/no question, you would be better equipped to try and understand what that person was trying to tell you. You would know, for instance, if you needed a calculator and a set of equations or if you needed a dictionary or language interpreter to input the data into to understand it.

This is a simplified way to understand data types, but it serves the purpose for this article. After all, knowing how data types work is not as important as know what data types are and what they represent.

Editor’s Note: We covered best commenting practices in C# in a previous article.

C# Data Types

Data types in C# are categorized into two types. The first is value types, which contain an instance of a type. Value types include the simpler types of data types, such as integers, floats, Booleans, and cha. Don’t worry about what those mean at the moment – those terms will make sense shortly. Other value types are enums, structs, and Nullable.

Reference types, meanwhile, contain a reference to an instance of the type. This is an important distinction: value types essentially contain an actual instance of a type while reference types simply hold a reference point to the instance of the type. Think of it like two bags: one holds actual gold, while the other bag holds a map to the gold.

C# has 16 pre-defined data types, the main five of which include integral, floating-point, char, strings, and Boolean.

Of the numeric data types, there are two main groups:

Integers: Integer data types represent whole numbers. They can be positive or negative, but must not contain a decimal. Integer data types are known as int and long. Floating points: Floating-point types are essentially numbers that contain decimals. In C#, floating-point types include float and double data types.

Read: Working with Strings in C#

int Data Types in C#

int data types are used to store whole numbers that range from 2147483647 to -2147483648. The majority of the time when you work with numeric values in C#, you will be using the int data type.

To create an int is pretty simple. You can >i>declare your int in the following manner:

using System;

namespace IntExample
{
  class IntApplication
  {
	static void Main(string[] args)
	{
  	int myAge = 44;
      Console.WriteLine(myAge);
	}
  }
}

The portion of the code above where you declare your variable is:

int myAge = 44;

Long Data Types in C#

A long is another numeric data type C# has to offer. As the name implies, it is used to store whole number like an int, only it stores a larger range of values. Namely, you can store values from 9223372036854775807 to -9223372036854775808. You only use this type of data type when the number is too large for an int. Here is an example of how you declare and use a long value in C#:

using System;

namespace LongApp
{
  class ExampleApplication
  {
	static void Main(string[] args)
	{
  	long myLotto = 85000000000L;
      Console.WriteLine(myLotto);
	}
  }
}

In this example, the long is assigned a value in the line:

  	long myLotto = 85000000000L;

Note the use of the L at the end of the numeric value; you must always include the L when making a long.

Read: Working with Math Operators in C#

float Data Types in C#

float data types are used to store decimal point or floating point numbers. They can store fractional numbers up to 7 decimal points long ranging from 3.4e−038 to 3.4e+038. Note that the numbers you store in a float must end with the letter F. Here is how you create a float in C#:

using System;

namespace FloatApp
{
  class ExampleApp
  {
	static void Main(string[] args)
	{
  	float mySalary = 150.50F;
      Console.WriteLine("My salary per hour is: " + mySalary);
	}
  }
}

This code would result in the output:

My salary per hour is 150.5

double Data Types in C#

double data types in C# are used to store floating point numbers that are too large for a float. You can store values ranging from 1.7e−308 to 1.7e+308 in a double. Best practices dictate that a double end in the letter D. Here is an example of how to create a double in C#:

using System;

namespace DoubleApp
{
  class ExampleApp
  {
	static void Main(string[] args)
	{
  	double mySalary = 150.50D;
      Console.WriteLine("My salary per hour is: " + mySalary);
	}
  }
}

Read: Tips for Writing Clean Code in C#

char Data Types in C#

There are data types in C# that handle text as well as numbers. The first of these is the char data type, which is used to store a single character. To create one, make sure you character is encased by a pair of single quotes, such as ‘A’. You can store any letter, number, or special character as a char value, so long as it is surrounded by single quotes.

Here is an example of how to create a char in C#:

using System;

namespace CharApp
{
  class CharExample
  {
	static void Main(string[] args)
	{
  	char myInitial = 'J';
  	char mySecondInitial = 'P';
      Console.WriteLine("My initials are: " + myInitial + mySecondInitial);
	}
  }
}

This code results in the following output:

My initials are: JP

string Data Types in C#

The other text data type in C# is known as a string. strings are used to store longer sequences of characters than a char. Instead of using single quotes to define them, strings are created using double-quotes. Here is some sample code showing how to create a string in C#:

using System;

namespace StringApp
{
  class StringExample
  {
	static void Main(string[] args)
	{
  	string firstApp = "Hello, World!";
      Console.WriteLine(firstApp);
	}
  }
}

The above code results in the output:

Hello, World!

 

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read