Skip to main content

Primitives Data Types and None-Primitives Data Types in C# with Code Examples

 

Primitives Data Types and None-Primitives Data Types in C# with Code Examples by Ziggy Rafiq

Overview

I wrote this post to provide an explanation of primitive and non-primitive data types in C#. C# is a strongly typed programming language, where each variable and expression must have a specific data type. C# data types are categorized into two primary groups: primitive data types and non-primitive data types.

Primitive data types are the simplest data types available in programming languages. They are typically pre-defined data types and can represent a single value, such as a boolean value, character, or integer. Examples of primitive data types include int, char, float, double, and boolean, which are common in programming languages like C++, C, and Java.

Non-primitive data types are also referred to as composite data types or reference data types. They are constructed from primitive data types and are more complex than primitive data types. Non-primitive data types can hold multiple values and allow for the creation of more intricate data structures like tables, lists, and graphs. Examples of non-primitive data types include arrays, strings, objects, classes, and structures, which are prevalent in programming languages such as C++, C, and Java. These data types are particularly useful in storing and manipulating large amounts of data.

What are Primitives Data Types in C#

In C#, primitive data types are the fundamental data types that represent simple values and are built into the language. These data types serve as the basic foundation for all programming languages. They are pre-defined data types that can be used directly by programmers without prior definitions. C# offers eight primitive data types that are classified into four groups: integral types, floating-point types, decimal type, and boolean type. In this post, we will examine each of these primitive data types in detail, including code examples.

Boolean

The bool data type is utilized to signify a binary value of either true or false. In C#, bool is the keyword that represents the Boolean data type. The bool data type can be employed to store variables that exhibit a true/false behaviour. For example, the boolean data type is a binary data type that can have two values: true or false. In C#, the bool keyword is used to denote this data type. Consider the following example, which demonstrates the declaration of a Boolean variable below.

Primitives Data Types in C# Boolean by Ziggy Rafiq

Numeric Types

C# has several numeric data types, which can be divided into two groups: integral types and floating-point types.

Integral Types

integral types are utilized to represent whole numbers, regardless of their sign. These types are comprised of four distinct categories: byte, short, int, and long, each with its own range of values and memory size. The signed or unsigned nature of each type specifies whether negative values can be represented or not. The smallest of these integral types is byte, while the largest is long.


  • byte: Represents an 8-bit unsigned integer.
  • int: Represents a 32-bit signed integer.
  • long: Represents a 64-bit signed integer.
  • sbyte: Represents an 8-bit signed integer.
  • short: Represents a 16-bit signed integer.
  • uint: Represents a 32-bit unsigned integer.
  • ulong: Represents a 64-bit unsigned integer.
  • ushort: Represents a 16-bit unsigned integer.

the below code example I wrote shows how we can declare the variables of each of these integral types in C#.

Primitives Data Types in C# Numeric Types Integral Types by Ziggy Rafiq


Floating-Point Types

Floating-Point types are utilized to represent numbers with decimal fractions. These types are classified as either single-precision or double-precision and are represented by two data types: float and double. The float type is a 32-bit single-precision floating-point type, while the double type is a 64-bit double-precision floating-point type.

  • double: Represents a double-precision floating-point number.
  • float: Represents a single-precision floating-point number.

The code below I wrote shows Floating-Point Types, which are double and float.

Numeric Types Numeric Types Floating-Point Types by Ziggy Rafiq

Decimal Type

The decimal type is a data type that represents decimal numbers with higher precision and a smaller range than floating-point types. In C#, the decimal type is represented by the keyword "decimal". 

The code below I wrote shows the Decimal Types variable.

Primitives Data Types in C# Numeric Types Decimal Type by Ziggy Rafiq

This a reminder that the decimal type requires a suffix "m" to distinguish it from other data types.

Character Types

Character types are considered a category of primitive data types. They are used to represent individual characters, such as letters, digits, and symbols. In C#, there is only one character type, which is called char.

The char data type represents a single 16-bit Unicode character. Unicode is a character encoding system that assigns a unique number to each character in most of the world's writing systems, including Latin, Greek, Cyrillic, Arabic, Hebrew, Chinese, Japanese, and Korean.

The code below I wrote  shows how to declare a variable of the char type and assign it a character value.

 
Primitives Data Types in C# Character Types by Ziggy Rafiq

 Other Types

There are also other types of Primitives Data Types apart from integral and floating-point types, C# also includes a few other primitive data types. These include being following below.

  • DateTime Type: The DateTime data type represents a date and time value. This type is commonly used in applications that work with dates and times, such as calendars and scheduling systems.
  •  TimeSpan Type: The TimeSpan data type represents a time interval or the duration between two points in time. This type is commonly used in applications that need to measure time intervals, such as timers and stopwatches.
The code below I are shows how we can declare variables of these DateTime and TimeSpan primitive data types in C#.

The code below I are shows how we can declare variables of these DateTime and TimeSpan primitive data types in C# the variables of the DateTime, and TimeSpan data types are declared and assigned values. Note that the DateTime value is initialized using the "new" keyword and the DateTime constructor.

By understanding and utilizing these other primitive data types, you can create more versatile and precise programs in C#.

of Primitives Data Types apart from integral and floating-point types, C# also includes a few other primitive data types by Ziggy Rafiq

What are None-Primitive Types in C#

Non-primitive data types, also referred to as reference types in C#, are more intricate data structures and objects that are created using class, struct, and interface definitions. These data types are used to represent complex data structures and hold multiple values. Examples of non-primitive data types in C# include arrays, classes, interfaces, and structures. Unlike primitive data types that have a fixed size, non-primitive data types can have varying sizes and are created using primitive data types.

Class

A class is a user-defined data type that serves as a blueprint for creating objects that share similar attributes and behaviours. It contains a set of properties and methods that the objects can inherit. Classes are reference types, which implies that they are stored on the heap and are accessed via reference.

We can create a class in C# by using the class keyword. The code example I have done below is showing us how to create a basic simple class in C# using the class keyword.

In the example below I have defined a class called Car with three properties Make, Model, and Year. We also define a method called StartEngine that prints a message to the console.

A class is a user-defined data type that serves as a blueprint for creating objects that share similar attributes and behaviours by Ziggy Rafiq

Once we have created a class, we can create an object of that class using the new keyword and use its properties and methods as follows below example shows.

we have created a class, we can create an object of that class using the new keyword and use its properties and methods as follows below example shows by Ziggy Rafiq

I have created an instance of the Car class and set its Make, Model, and Year properties. Then, I call the StartEngine method of the object.

By using classes, we can create reusable code that can be easily maintained and modified. They are an essential feature of object-oriented programming and can help you write clean, organized, and efficient code.

Struct/ Structures

A struct is a user-defined data type that defines a set of properties and methods, similar to a class. However, unlike classes, structs are value types, which means they are stored on the stack and passed by value.

We can create a struct in C#, we need to use the struct keyword. Below is an example of how to create a simple struct in C#.

In the example below I have defined a struct called Person with three properties FirstName, LastName, and Age. We also define a method called SayHello that prints a message to the console.

A struct is a user-defined data type that defines a set of properties and methods, similar to a class.  by Ziggy Rafiq

Once we have defined a struct, we can create an instance of that struct using the new keyword and use its properties and methods as follows code example below.

Once we have defined a struct, we can create an instance of that struct using the new keyword and use its properties and methods as follows code example below by Ziggy Rafiq

We create an instance of the Person struct and set its FirstName, LastName, and Age properties. Then, we call the SayHello method of the object.

By using structs, you can create lightweight data types that are useful for small data structures and simple calculations. They are passed by value, which can improve performance, but they should not be used for large or complex data structures.

Interface

An interface is a user-defined type that defines a set of methods, properties, and events that a class or struct must implement. Interfaces are created using the keyword "interface". An interface is like a contract between the interface and the class or struct that implements it, specifying what the class or struct can do without specifying how it does it.

In this example of creating an interface in C#, in which we have created an interface named IShape. The IShape interface specifies two methods that any class or struct that implements the interface must provide implementations for: GetArea() and GetPerimeter().

interface in C#, in which we have created an interface named IShape. The IShape interface specifies two methods that any class or struct that implements the interface must provide implementations for: GetArea() and GetPerimeter() by Ziggy Rafiq

By using interfaces, we can define a common set of methods that multiple classes or structs can implement. This allows for greater flexibility in our code and makes it easier to write reusable and maintainable software.

Array

An array is a data structure that can store a collection of values of the same data type. It can be either single-dimensional or multi-dimensional. In C#, arrays are created using the keyword "array". To create an array, you need to specify the data type of the elements that the array will hold, followed by the name of the array and the number of elements it will contain. The following code demonstrates how to declare and initialize an array of integers:

In the code example below an int is the data type of the elements numberArray is the name of the array, and 5 is the number of elements that the array can hold.

An array is a data structure that can store a collection of values of the same data type by Ziggy Rafiq

Enum

An enum is a user-defined data type that represents a set of named values. It is used to declare a set of related constants that can be assigned to a variable or used as a parameter in a method. Enumerations are created using the keyword "enum". The following code below shows how to create a simple enumeration in C# by using the keyword “enum”.

Enum in C# Above I have created an enumeration called "Colours" with four named values: "Red", "Blue", "Green", and "Yellow". We can use this enumeration to create variables that can hold one of these four values.  by Ziggy Rafiq

Above I have created an enumeration called "Colours" with four named values: "Red", "Blue", "Green", and "Yellow". We can use this enumeration to create variables that can hold one of these four values.  A code example below shows that.

Enum Colours in C# by Ziggy Rafiq

I have created a variable called "colours" of type "Colours" and assigned it the value

"Green".


Summary

C# has a wide range of data types, from the simple and basic to the more complex and specialized. Understanding the differences between primitive and non-primitive data types is an important part of writing effective C# code. By using the appropriate data type for each situation, you can write code that is efficient, maintainable, and easy to read. I hope you all have enjoyed reading this post as I have enjoyed writing it tonight.

Comments

Most Viewed Ziggy Rafiq Blog Posts

A Complete Guide to Using GUIDs in C# with Code Examples

  Overview In this post, we are looking at GUIDs (Globally Unique Identifiers), which are widely used in C# for generating unique identifiers for objects, entities, and resources in a system. In this post, we'll explore the basics of GUIDs in C#, their advantages, and how to convert strings to GUIDs. In this post, we have used Guid Generator to create the GUID from the following URL Address https://guidgenerator.com/ What is GUID GUID (Globally Unique Identifier) in C# is a 128-bit value that is used to identify objects, entities, or resources in a unique manner across different systems and applications. It is also known as UUID (Universally Unique Identifier) in some other programming languages.   GUIDs are generated using a combination of unique factors such as the MAC address of the network adapter, the current time and date, and a random number. The resulting GUID is a string of 32 hexadecimal digits separated by hyphens, such as "b86f2096-237a-4059-8329-1bbcea72769b&

How to use Enum Data Values with .Net 6.0 Framework and Entity Framework Core 6

How to use Enum Data Values with .Net 6.0 Framework and Entity Framework Core 6 Overview An Enum (Enumeration) is a group of constants that are read-only value types. By default, the first value of the Enum variable is 0 i.e. Here we will create an Enum of Priorities type with read-only values of Highest, Normal and Low. We will set the read-only values using an integer assigning a number next to the value. By default, the integer value will start with 0. Here we will be assigning the integer value next to the Enum value such as in the below example and we will use a comma (,) to separate the item in the list of Enum(Enumeration).  We create Enum by using the Enum keyword and then using class, interface, and abstract. The reason we use an Enum is to ensure we improve our application performance and improve application readability, and maintainability, and reduces the complexity of the application hence why if you take a look at the example below of Status (NotStarted, Started, Complete