Built-in Types in C#
These types are built into C# as most of them have usefulness across a wide array of applications.
1. Object
- The base type for all data types.
- Can hold any data type, including user-defined types.
ALL types inherit from object
. More on this later.
2. String/Char
String: A sequence of characters.
string myString = "Hello, World!";
Char: A single character literal.
char myChar = 'A';
3. Boolean
Represents true or false values.
bool myBool = true;
4. Byte/Sbyte
Byte: 8-bit unsigned integer.
Sbyte: 8-bit signed integer.
byte myByte = 255;
sbyte mySbyte = -128;
5. Int/Short/Long
Int: 32-bit signed integer. Min value is int.MinValue
and max value is int.MaxValue
, which are -2147483648 and 2147483647, respectively.
int myInt = 2147483647;
Short: 16-bit signed integer. Min value is short.MinValue
and max value is short.MaxValue
, which are -32768 and 32767, respectively.
short myShort = 32767;
Long: 64-bit signed integer. Min value is long.MinValue
and max value is long.MaxValue
, which are -9223372036854775808 and 9223372036854775807, respectively.
long myLong = 9223372036854775807;
6. Unsigned Int/Short/Long
Unsigned Int: 32-bit unsigned integer. Min value is uint.MinValue
and max value is uint.MaxValue
, which are 0 and 4294967295, respectively.
Unsigned Short: 16-bit unsigned integer. Min value is ushort.MinValue
and max value is ushort.MaxValue
, which are 0 and 65535, respectively.
Unsigned Long: 64-bit unsigned integer. Min value is ulong.MinValue
and max value is ulong.MaxValue
, which are 0 and 18446744073709551615, respectively.
uint myUint = 4294967295;
ushort myUshort = 65535;
ulong myUlong = 18446744073709551615;
7. Float/Double/Decimal
Float: 32-bit floating point number.
Double: 64-bit floating point number.
Decimal: 128-bit precise decimal value.
float myFloat = 3.14f;
double myDouble = 3.141592653589793;
decimal myDecimal = 3.14159265358979323846264338327950288419716939937510582097494459m;
I tend to use decimal
for most decimal representations for consistency.
8. DateTime
Used for representing dates and times. These are immutable values.
DateTime myDateTime = DateTime.UtcNow;
DateTime
has a bunch of important methods and properties:
DateTime.Now
: Gets the current date and time on the local machine.DateTime.UtcNow
: Gets the current date and time in UTC.DateTime.Day
: Gets the day of the month.DateTime.Month
: Gets the month of the year.DateTime.Year
: Gets the year.DateTime.Hour
: Gets the hour of the day.DateTime.Minute
: Gets the minute of the hour.DateTime.Second
: Gets the second of the minute.DateTime.Millisecond
: Gets the millisecond of the second.DateTime.DayOfWeek
: Gets the day of the week.DateTime.DayOfYear
: Gets the day of the year.DateTime.TimeOfDay
: Gets the time of day.DateTime.AddDays()
: Adds a specified number of days to the date and time.DateTime.AddHours()
: Adds a specified number of hours to the date and time.DateTime.AddMinutes()
: Adds a specified number of minutes to the date and time.DateTime.AddSeconds()
: Adds a specified number of seconds to the date and time.DateTime.AddMilliseconds()
: Adds a specified number of milliseconds to the date and time.DateTime.Parse()
: Parses a string representation of a date and time into aDateTime
object.DateTime.TryParse()
: Attempts to convert a string representation of a date and time into aDateTime
object and returns a boolean indicating whether the conversion succeeded.DateTime.TryParseExact()
: Attempts to convert a string representation of a date and time into aDateTime
object using a specified format and culture-specific format information, and returns a boolean indicating whether the conversion succeeded.DateTime.TryParseExact()
: Attempts to convert a string representation of a date and time into aDateTime
object using a specified format and culture-specific format information, and returns a boolean indicating whether the conversion succeeded.DateTime.TryParseExact()
: Attempts to convert a string representation of a date and time into aDateTime
object using a specified format and culture-specific format information, and returns a boolean indicating whether the conversion succeeded.
🌶️🌶️🌶️ If you're using DateTime.Now
, you're using the local time of the machine that's running the code. If you're using DateTime.UtcNow
, you're using the current date and time in UTC (Coordinated Universal Time). Avoid using DateTime.Now
in pretty much all cases for production code.
9. Arrays
A collection of items of the same type, stored in contiguous memory.
int[] myIntArray = new int[3];
myIntArray[0] = 1;
myIntArray[1] = 2;
myIntArray[2] = 3;
The []
syntax is called the "indexer" - it's a way to access elements in the array.
Arrays have a few important properties:
myIntArray.Length
: Gets the number of elements in the array.myIntArray.Rank
: Gets the number of dimensions in the array. (You can have multi-dimensional arrays declared like this:int[,] myIntArray = new int[3, 3];
, but we'll mostly be working with single-dimensional arrays.)
10. Enums
A set of named constants.
enum EmployeeType
{
Manager,
Supervisor,
Worker
}
Enums have an "underlying" type - the default is int
. Each of these enum values have values automatically assigned to them, starting with 0. You can also manually define values like so:
enum EmployeeType
{
Manager = 1,
Supervisor = 2,
Worker = 10 //note the values don't have to be sequential, but I try to make them sequential as much as possible
}
🌶️🌶️🌶️ I only bother assigning explicit underlying values to enums if they're stored in a database somewhere.
11. Tuples
Tuples in C# are versatile, lightweight data structures that can group multiple values together, even if they are of different types. They are particularly useful when you want to return more than one value from a method or quickly bundle values without creating a custom class or struct.
Creating Tuples
You can define a tuple using the tuple literal syntax with parentheses:
var myTuple = (42, "Hello", true);
Console.WriteLine(myTuple.Item1); // Output: 42
Console.WriteLine(myTuple.Item2); // Output: Hello
Console.WriteLine(myTuple.Item3); // Output: true
Named Tuples
C# allows you to assign meaningful names to the tuple elements, making your code more readable.
var personInfo = (Age: 30, Name: "Alice", IsEmployed: true);
Console.WriteLine(personInfo.Age); // Output: 30
Console.WriteLine(personInfo.Name); // Output: Alice
Console.WriteLine(personInfo.IsEmployed); // Output: true
Deconstructing Tuples
You can deconstruct tuples into individual variables using the deconstruction syntax:
var (age, name, isEmployed) = personInfo;
Console.WriteLine(age); // Output: 30
Console.WriteLine(name); // Output: Alice
Console.WriteLine(isEmployed); // Output: true
Use Cases
Returning multiple values from a method:
public (int, string) GetPerson()
{
return (25, "Bob");
}
var (age, name) = GetPerson();
I like to give the tuple values meaningful names.
public (int Age, string Name) GetPerson()
{
return (25, "Bob");
}
var person = GetPerson();
Console.WriteLine(person.Name);
Ad-hoc grouping of values: Tuples can be used to group temporary, related data without the need to define a separate class or struct.
12. Special Mention: Null and Void
Null:
A special marker for "no value." Doesn't have a type per se.
Employee employee = null;
More on this later - nullability is a big topic!
Void:
Represents no return value from a method.
void MyMethod()
{
Console.WriteLine("Hello, World!");
}
Fun fact: void
is actually an alias for System.Void
and is an actual type in C#, but not one you can use directly (believe me, I've tried... for fun of course. ;)