Data types specify the type of data that a valid C# variable can hold. C# is a strongly typed programming language because in C# each type of data (such as integer, character, float, and so forth) is predefined as part of the programming language and all constants or variables defined for a given program must be described with one of the data types.
Data Types in C# is Mainly Divided into 3 Categories:
- Value Data Types
- Reference Data Types
- Pointer Data Type
1. Value Data Types
In C#, the Value Data Types will directly store the variable value in memory and it will also accept both signed and unsigned literals. The derived class for these data types are System.ValueType. Following are different Value Data Types in C# programming language
1.1 Signed & Unsigned Integral Types
There are 8 integral types which provide support for 8-bit, 16-bit, 32-bit, and 64-bit values in signed or unsigned form.
Alias | Type Name | Type | Size(bits) | Range | Default Value |
---|
sbyte | System.Sbyte | signed integer | 8 | -128 to 127 | 0 |
short | System.Int16 | signed integer | 16 | -32768 to 32767 | 0 |
Int | System.Int32 | signed integer | 32 | -231 to 231-1 | 0 |
long | System.Int64 | signed integer | 64 | -263 to 263-1 | 0L |
byte | System.byte | unsigned integer | 8 | 0 to 255 | 0 |
ushort | System.UInt16 | unsigned integer | 16 | 0 to 65535 | 0 |
uint | System.UInt32 | unsigned integer | 32 | 0 to 232 | 0 |
ulong | System.UInt64 | unsigned integer | 64 | 0 to 263 | 0 |
1.2 Floating Point Types
There are 2 floating point data types which contain the decimal point.
Alias | Type name | Size(bits) | Range (aprox) | Default Value |
---|
float | System.Single | 32 | ±1.5 × 10-45 to ±3.4 × 1038 | 0.0F |
double | System.Double | 64 | ±5.0 × 10-324 to ±1.7 × 10308 | 0.0D |
- Float: It is 32-bit single-precision floating point type. It has 7 digit Precision. To initialize a float variable, use the suffix f or F. Like, float x = 3.5F;. If the suffix F or f will not use then it is treated as double.
- Double:It is 64-bit double-precision floating point type. It has 14 - 15 digit Precision. To initialize a double variable, use the suffix d or D. But it is not mandatory to use suffix because by default floating data types are the double type.
1.3 Decimal Types
The decimal type is a 128-bit data type suitable for financial and monetary calculations. It has 28-29 digit Precision. To initialize a decimal variable, use the suffix m or M. Like as, decimal x = 300.5m;. If the suffix m or M will not use then it is treated as double.
Alias | Type name | Size(bits) | Range (aprox) | Default value |
---|
decimal | System.Decimal | 128 | ±1.0 × 10-28 to ±7.9228 × 1028 | 0.0M |
1.4 Character Types
The character types represents a UTF-16 code unit or represents the 16-bit Unicode character.
Alias | Type name | Size In(Bits) | Range | Default value |
---|
char | System.Char | 16 | U +0000 to U +ffff | '\0' |
Example 1:
C#
// C# program to demonstrate
// the above data types
using System;
namespace ValueTypeTest {
class GeeksforGeeks {
// Main function
static void Main()
{
// declaring character
char a = 'G';
// Integer data type is generally
// used for numeric values
int i = 89;
short s = 56;
// this will give error as number
// is larger than short range
// short s1 = 87878787878;
// long uses Integer values which
// may signed or unsigned
long l = 4564;
// UInt data type is generally
// used for unsigned integer values
uint ui = 95;
ushort us = 76;
// this will give error as number is
// larger than short range
// ulong data type is generally
// used for unsigned integer values
ulong ul = 3624573;
// by default fraction value
// is double in C#
double d = 8.358674532;
// for float use 'f' as suffix
float f = 3.7330645f;
// for float use 'm' as suffix
decimal dec = 389.5m;
Console.WriteLine("char: " + a);
Console.WriteLine("integer: " + i);
Console.WriteLine("short: " + s);
Console.WriteLine("long: " + l);
Console.WriteLine("float: " + f);
Console.WriteLine("double: " + d);
Console.WriteLine("decimal: " + dec);
Console.WriteLine("Unsinged integer: " + ui);
Console.WriteLine("Unsinged short: " + us);
Console.WriteLine("Unsinged long: " + ul);
}
}
}
Outputchar: G
integer: 89
short: 56
long: 4564
float: 3.733064
double: 8.358674532
decimal: 389.5
Unsinged integer: 95
Unsinged short: 76
Unsinged long: 3624573
Example 2:
C#
// Sbyte signed integral data type
using System;
namespace ValueTypeTest {
class GeeksforGeeks {
// Main function
static void Main()
{
sbyte a = 126;
// sbyte is 8 bit
// singned value
Console.WriteLine(a);
a++;
Console.WriteLine(a);
// It overflows here because
// byte can hold values
// from -128 to 127
a++;
Console.WriteLine(a);
// Looping back within
// the range
a++;
Console.WriteLine(a);
}
}
}
Example 3:
C#
// C# program to demonstrate
// the byte data type
using System;
namespace ValueTypeTest {
class GeeksforGeeks {
// Main function
static void Main()
{
byte a = 0;
// byte is 8 bit
// unsigned value
Console.WriteLine(a);
a++;
Console.WriteLine(a);
a = 254;
// It overflows here because
// byte can hold values from
// 0 to 255
a++;
Console.WriteLine(a);
// Looping back within the range
a++;
Console.WriteLine(a);
}
}
}
1.5 Boolean Types
It has to be assigned either true or false value. Values of type bool are not converted implicitly or explicitly (with casts) to any other type. But the programmer can easily write conversion code.
Alias | Type Name | Possible Values |
---|
bool | System.Boolean | true / false |
---|
Example:
C#
// Using Boolean data type
using System;
namespace ValueTypeTest {
class GeeksforGeeks {
// Main function
static void Main()
{
// boolean data type
bool b = true;
if (b == true)
Console.WriteLine("Hi Geek");
}
}
}
2. Reference Data Types
The Reference Data Types will contain a memory address of variable value because the reference types won’t store the variable value directly in memory. When you create a reference type variable, such as an object or a string, you are actually storing a reference (or pointer) to the location in memory where the data is held. The actual data for reference types is stored on the heap. The heap is a large pool of memory used for dynamic memory allocation. The built-in reference types are string, object.
2.1 String
It represents a sequence of Unicode characters and its type name is System.String. So, string and String are equivalent.
Example:
string s1 = "hello"; // creating through string keyword
String s2 = "welcome"; // creating through String class
2.2 Object
In C#, all types, predefined and user-defined, reference types and value types, inherit directly or indirectly from Object. So basically it is the base class for all the data types in C#. Before assigning values, it needs type conversion. When a variable of a value type is converted to object, it's called boxing. When a variable of type object is converted to a value type, it's called unboxing. Its type name is System.Object.
Example:
C#
// Using Reference data types
using System;
namespace ValueTypeTest {
class Geeks {
// Main Function
static void Main()
{
// declaring string
string a = "Geeks";
// append in a
a += "for";
a = a + "Geeks";
Console.WriteLine(a);
// declare object obj
object obj;
obj = 20;
Console.WriteLine(obj);
// to show type of object
// using GetType()
Console.WriteLine(obj.GetType());
}
}
}
OutputGeeksforGeeks
20
System.Int32
3. Pointer Data Type
The Pointer Data Types will contain a memory address of the variable value. To get the pointer details we have a two symbols ampersand (&) and asterisk (*).
- ampersand (&): It is known as Address Operator. It is used to determine the address of a variable.
- asterisk (*): It also known as Indirection Operator. It is used to access the value of an address.
Syntax:
type* identifier;
Example:
// Valid syntax
int* p1, p;
// Invalid
int *p1, *p;
Note: This program will not work on online compiler Error: Unsafe code requires the `unsafe' command line option to be specified. For its solution: Go to your project properties page and check under Build the checkbox Allow unsafe code.
Implementation:
C#
// Using Pointer Data Type
using System;
namespace Pointerprogram {
class GFG {
// Main function
static void Main()
{
unsafe
{
// declare variable
int n = 10;
// store variable n address
// location in pointer variable p
int* p = &n;
Console.WriteLine("Value :{0}", n);
Console.WriteLine("Address :{0}", (int)p);
}
}
}
}
Output:
Value :10
Address :1988374520
Similar Reads
Introduction
C# TutorialC# (pronounced "C-sharp") is a modern, versatile, object-oriented programming language developed by Microsoft in 2000 that runs on the .NET Framework. Whether you're creating Windows applications, diving into Unity game development, or working on enterprise solutions, C# is one of the top choices fo
4 min read
Introduction to .NET FrameworkThe .NET Framework is a software development framework developed by Microsoft that provides a runtime environment and a set of libraries and tools for building and running applications on Windows operating systems. The .NET framework is primarily used on Windows, while .NET Core (which evolved into
6 min read
C# .NET Framework (Basic Architecture and Component Stack)C# (C-Sharp) is a modern, object-oriented programming language developed by Microsoft in 2000. It is a part of the .NET ecosystem and is widely used for building desktop, web, mobile, cloud, and enterprise applications. This is originally tied to the .NET Framework, C# has evolved to be the primary
6 min read
C# Hello WorldThe Hello World Program is the most basic program when we dive into a new programming language. This simply prints "Hello World!" on the console. In C#, a basic program consists of the following:A Namespace DeclarationClass Declaration & DefinitionClass Members(like variables, methods, etc.)Main
4 min read
Common Language Runtime (CLR) in C#The Common Language Runtime (CLR) is a component of the Microsoft .NET Framework that manages the execution of .NET applications. It is responsible for loading and executing the code written in various .NET programming languages, including C#, VB.NET, F#, and others.When a C# program is compiled, th
4 min read
Fundamentals
C# IdentifiersIn programming languages, identifiers are used for identification purposes. Or in other words, identifiers are the user-defined name of the program components. In C#, an identifier can be a class name, method name, variable name, or label. Example: public class GFG { static public void Main () { int
2 min read
C# Data TypesData types specify the type of data that a valid C# variable can hold. C# is a strongly typed programming language because in C# each type of data (such as integer, character, float, and so forth) is predefined as part of the programming language and all constants or variables defined for a given pr
7 min read
C# VariablesIn C#, variables are containers used to store data values during program execution. So basically, a Variable is a placeholder of the information which can be changed at runtime. And variables allows to Retrieve and Manipulate the stored information. In Brief Defination: When a user enters a new valu
4 min read
C# LiteralsIn C#, a literal is a fixed value used in a program. These values are directly written into the code and can be used by variables. A literal can be an integer, floating-point number, string, character, boolean, or even null. Example:// Here 100 is a constant/literal.int x = 100; Types of Literals in
5 min read
C# OperatorsIn C#, Operators are special types of symbols which perform operations on variables or values. It is a fundamental part of language which plays an important role in performing different mathematical operations. It takes one or more operands and performs operations to produce a result.Types of Operat
7 min read
C# KeywordsKeywords or Reserved words are the words in a language that are used for some internal process or represent some predefined actions. These words are therefore not allowed to be used as variable names or objects. Doing this will result in a compile-time error.Example:C#// C# Program to illustrate the
5 min read
Control Statements
C# Decision Making (if, if-else, if-else-if ladder, nested if, switch, nested switch)Decision Making in programming is similar to decision making in real life. In programming too, a certain block of code needs to be executed when some condition is fulfilled. A programming language uses control statements to control the flow of execution of program based on certain conditions. These
5 min read
C# Switch StatementIn C#, Switch statement is a multiway branch statement. It provides an efficient way to transfer the execution to different parts of a code based on the value of the expression. The switch expression is of integer type such as int, char, byte, or short, or of an enumeration type, or of string type.
4 min read
C# LoopsLooping in a programming language is a way to execute a statement or a set of statements multiple times, depending on the result of the condition to be evaluated to execute statements. The result condition should be true to execute statements within loops.Types of Loops in C#Loops are mainly divided
4 min read
C# Jump Statements (Break, Continue, Goto, Return and Throw)In C#, Jump statements are used to transfer control from one point to another point in the program due to some specified code while executing the program. In, this article, we will learn to different jump statements available to work in C#.Types of Jump StatementsThere are mainly five keywords in th
4 min read
OOP Concepts
Methods
Arrays
C# ArraysAn array is a group of like-typed variables that are referred to by a common name. And each data item is called an element of the array. The data types of the elements may be any valid data type like char, int, float, etc. and the elements are stored in a contiguous location. Length of the array spe
8 min read
C# Jagged ArraysA jagged array is an array of arrays, where each element in the main array can have a different length. In simpler terms, a jagged array is an array whose elements are themselves arrays. These inner arrays can have different lengths. Can also be mixed with multidimensional arrays. The number of rows
4 min read
C# Array ClassArray class in C# is part of the System namespace and provides methods for creating, searching, and sorting arrays. The Array class is not part of the System.Collections namespace, but it is still considered as a collection because it is based on the IList interface. The Array class is the base clas
7 min read
How to Sort an Array in C# | Array.Sort() Method Set - 1Array.Sort Method in C# is used to sort elements in a one-dimensional array. There are 17 methods in the overload list of this method as follows:Sort<T>(T[]) MethodSort<T>(T[], IComparer<T>) MethodSort<T>(T[], Int32, Int32) MethodSort<T>(T[], Comparison<T>) Method
8 min read
How to find the rank of an array in C#Array.Rank Property is used to get the rank of the Array. Rank is the number of dimensions of an array. For example, 1-D array returns 1, a 2-D array returns 2, and so on. Syntax: public int Rank { get; } Property Value: It returns the rank (number of dimensions) of the Array of type System.Int32. B
2 min read
ArrayList
String
Tuple
Indexers