Introduction To C# Data Types
Data in computer programming possess various attributes. One of the major attributes is the data type. It tells the compiler or interpreter how the user wants to use the data. There are various basic data types used in almost all programming languages like integer, float, string/character, and Boolean. All these types have unique identity and data stored in that features the identity. Like, an integer data type variable can hold only integer value, a Boolean variable holds either true or false value. Being a strongly typed language, data types have enormous importance in C#. In strong typed language, the compiler strongly checks all operations for type compatibility. This checking is important.
Different C# Data Types
C# has two in-built data types:
- Value Data Type: A value Data Type variable contains an actual value such as a number or string, or fraction.
- Reference Data Type: This type of variable contains the address of another value variable.
1. Value Types
Like other programming languages, C#, too has some value types that are used to hold a value. These data types are derived from System.valueType class. The value type variables are normally stored on the stack are passed by copying them.
The below table illustrates the various value types in C#:
Data Type | Description | Size |
int | Holds value within the range -2,147,483,648 to 2,147,483,647 | 4 bytes |
long | Holds value within the range -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | 8 bytes |
float | Used to hold fractional value. Stores 6 to 7 decimal digits. | 4 bytes |
double | Used to hold fractional value. Stores up to 15 decimal digits. | 8 bytes |
decimal | Holds value within the range 1E-28 to 7.9E 28. Widely used in monetary calculations. | 16 bytes |
char | Holds single character that is encapsulated by single quotes | 2 bytes |
string | Stores an array of characters, encapsulated by double quotes | 2 bytes per character |
bool | Holds value either true or false | 1 bit |
byte | It is unsigned integer that ranges between 0 to 255 | 1 byte |
short | It is signed integer that holds value between -32768 to 32767 | 2 byte |
2. Reference Types
Reference type variable holds a reference to other value variables. These variables are stored on a heap and passed by creating a reference.
The below table illustrates the various reference types in C#:
Data Type | Description |
String | It represents a sequence of zero or more unicode characters. |
Arrays | It holds the number of elements. |
Delegate | It has a return value and has any number of parameters. |
Use Of Pointer Types
A pointer is not a new word in computer programming. A pointer holds the address of another variable. Like any other variable, the pointer variable must be declared before use. Pointer variables are declared with a slight difference as:
Syntax:
Type *variable_name;
Example:
Code:
int *a; // pointer to an integer
Examples Of C# Data Types
Given below are the examples of C# Data Types:
Example #1
Code:
Using System;
Using System.Text;
Using System.Collections.Generic;
Using System.Linq;
Using System.Threading.Tasks;
Namespace DatatypeInCSharp
{
Class Program
{
Static void Main(String[] args)
{
int var1=15; //integer variable
double var2=15.24; //fraction variable
Boolean var3=true; //Boolean variable
String var4= “C#”; // string variable
Console.WriteLine(“Integer variable:” var1);
Console.WriteLine(“Double variable:” var2);
Console.WriteLine(“Boolean variable:” var3);
Console.WriteLine(“String variable:” var4);
Console.Readkey();
}
}
}
Output:
Example #2
Reference Type.
Code:
Using System;
Namespace ReferenceTypeTest
{
Class RefTest
{
String str_var1= “Hello”; //string variable declaration
str_var1 = “World”; //appending strings
Console.WriteLine(str_var1);
Object obj_var; //object variable
obj_var=50;
Console.WriteLine(obj_var);
Console.WriteLine(obj_var.GetType());
}
}
Output:
Example #3
Pointer Type.
Code:
using System;
using System.Text;
using System.Linq;
using System.Collection.Generic;
namespace PointerVariable
{
Class Program
{
static void Main(string[] args)
{
int var_1;
Invalid
{
int var_1=50;
int* pointer_var=&var_1;
Console.WriteLine((int)pointer_var); //to display memory address
Console.WriteLine(*pointer_var); //Display the value at the memory
Console.ReadLine();
}
}
}
}
Output:
Conclusion
With these data types, you can use various types of variables in your project or application.
Recommended Articles
This is a guide to C# Data Types. Here we discuss the introduction, different C# data types, use of pointer types and examples respectively. You can also go through our other suggested articles to learn more –
Are you preparing for the entrance exam ?
Join our Programming Languages test series to get more practice in your preparation
View More