Data Types
Data Types
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.
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 :
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.
Size(bits Default
Alias Type Name Type ) Range Value
-32768 to
short System.Int16 signed integer 16 32767 0
unsigned
byte System.byte integer 8 0 to 255 0
System.UInt3 unsigned
uint 2 integer 32 0 to 232 0
Size(bits Default
Alias Type Name Type ) Range Value
System.UInt6 unsigned
ulong 4 integer 64 0 to 263 0
Floating Point Types :There are 2 floating point data types which
contain the decimal point.
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.
Alias Type name Size(bits) Range (aprox) Default Value
class GeeksforGeeks {
// Main function
static void Main()
{
// declaring character
char a = 'G';
short s = 56;
ushort us = 76;
// this will give error as number is
// larger than short range
3. Output :
4. char: G
5. integer: 89
6. short: 56
7. long: 4564
8. float: 3.733064
9. double: 8.358674532
10. decimal: 389.5
11. Unsinged integer: 95
12. Unsinged short: 76
13. Unsinged long: 3624573
14. Example :
// C# program to demonstrate the 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);
15. Output :
16. 126
17. 127
18. -128
19. -127
20. Example :
// 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;
21. Output :
22. 0
23. 1
24. 255
25. 0
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 Values
class GeeksforGeeks {
// Main function
static void Main()
{
Output :
Hi Geek
26. 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. The built-in reference types are string, object.
Example :
string s1 = "hello"; // creating through string keyword
String s2 = "welcome"; // creating through String class
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# program to demonstrate
// the Reference data types
using System;
namespace ValueTypeTest {
class GeeksforGeeks {
// Main Function
static void Main()
{
// declaring string
string a = "Geeks";
//append in a
a+="for";
a = a+"Geeks";
Console.WriteLine(a);
Output :
GeeksforGeeks
20
System.Int32
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 :
int* p1, p; // Valid syntax
int *p1, *p; // Invalid
Example :
// 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.
using System;
namespace Pointerprogram {
class GFG {
// Main function
static void Main()
{
unsafe
{
// declare variable
int n = 10;