C# Data Types
C# is a highly-defined language. You should declare the type for every variable and object. It means we have to declare the variable type, which indicates the type of values that it will store, such as integer, float, decimal, text, etc. A data type is a series of variables that you can use for certain values. Continue reading to know more about the different data types in C#.
Data Types in C#
There are three different data types in C#. They are -
- Value Data types
You can allocate a data value to the data form directly. All these data types are derived from System.ValueType. It may include signed and unsigned literal content. Some of the data value classes are int, float, and char that hold numbers, floating dot, and alphabets.
- Integer Types
A few integral forms available that are supported in signed or unsigned for 8-bit, 16-bit, 32-bit, and 64-bit values. These include Int, byte, long, short, uint, ulong, and short, etc.
- Floating-point types
There are two types of floating points containing the decimal point.
Float - It has a floating-point type with a single-precision of 32-bit. The accuracy is seven digits. Use the suffix for F to start a floating element. They can be treated as double if you do not use them for F.
Double - It is a 64-bit floating-point type with double precision. The precise number is 14-15 digits. Use the suffix d or D to start a double variable. However, the use of suffixes is not needed, as floating data types are double-type by nature.
- Character Types
The character type is a UTF-16 code unit or a 16-bit Unicode character. It can hold a single letter which is known as a character.
- Boolean Types
The boolean type can only take values known as true or false. It is represented using a bool keyword.
Example:
using System; Namespace ValueTypeTest { Class data types { static void Main() { char a = ‘G’; int i = 65; short s = 45; long l = 4564; uint ui = 87; ushort us = 76; ulong ul = 3624573; double d = 8.376585986; float f = 8.7330645f; decimal dec = 376.6m; Console.WriteLine(“char: “ + a); Console.WriteLine(“integer: “ + i); Console.WriteLine(“short: “ + s); Console.WriteLine(“long: “ + a); Console.WriteLine(“float: “ + a); Console.WriteLine(“double: “ + a); Console.WriteLine(“decimal: “ + a); Console.WriteLine(“Unsigned integer: “ + ui); Console.WriteLine(“Unsigned short: “ + a); Console.WriteLine(“char: “ + a); }
- Reference Data Types
The Reference Data Types have a variable value memory address since the variable value types will not store the variable in the memory.
- Object types
Each category in C# is derived directly or implicitly from the class type of an entity. Boxing and unboxing are used to handle the properties of various data types as objects. It is called boxing when the value type has to be modified and unboxing when it has to be modified to the value type.
Ex:
using System; Class Reference { static void Main () { int val1 = 200; object obj = val1; int val2 = (int)obj; } }
- Array types
Arrays are the same type of data collected. It is located in neighboring memory locations. The first element is the lowest address and the highest in the last one.
Ex:
using System; Class Reference { public static void Main (string[] args) { int [] y = new int [] { 1,2, 5,6}; for( int i=0; i< 4; i++) Console.WriteLine(y[i]); } }
- String types
A string is a collection of zero or more Unicode sequences in C#. The object class is derived as with C# forms. They are derived from the System. String. This data type allows you to assign a string value to a variable.
Ex:
using System; Class Reference { public static void Main () { String a = "Good Morning"; Console.WriteLine(a); } }
- Pointer Data Types
The C# pointer types save the address of a particular type of data. They are used in an unsafe way, i.e. the software needs an unsafe modification to use pointers.
There are two symbols used in pointers. The ampersand sign (&) is an address operator used to define the address of the variable. The asterisk (*) is an indirection operator that allows the access of an address.
Ex:
using System; namespace Pointer { Class Pointer { static unsafe void Main (string[] args) { int Val = 150; int* ptr = &Val; Console.WriteLine(" Data value is: {0}", Val); } } }
Other useful articles:
- TOP-10 Basic C# Interview Questions and Answers
- TOP-10 C# Questions for Advanced Programmers
- 10 Important C# Interview Questions and Answers
- Most Asked C# Interview Q & A
- C# Interview for Beginners
- TOP-10 Advanced C# Interview Questions and Answers
- Introduction to Functions in C#
- C# Jagged Arrays
- C Sharp Interview Questions for 5 Years Experience
- C Sharp Interview Questions for Testers
- C Sharp Questions for 10 Years Experience
- Iteration Statements in C#
- Exception Handling in C#
- C Sharp Methods
- C# Data Types
- C# Vs. C++
- Regular Expressions in C Sharp
- Defining Nested Methods in C Sharp
- C Sharp Dynamic Data Type
- C Custom Exceptions
- How to Generate Random Numbers with C Sharp
- Pass by Value vs Pass by Reference in C Sharp