C# data type


Release date:2023-08-23 Update date:2023-10-13 Editor:admin View counts:410

Label:

C# data type

In C#, variables are divided into the following types:

  • Value type

  • Reference type

  • Pointer type

Value type

A value type variable can be assigned directly to a value. They are derived from the class System.ValueType.

The value type contains data directly, such as intcharfloat that store numbers, characters, and floating-point numbers, respectively when you declare a int type, the system allocates memory to store values.

The following table lists the value types available in C# 2010:

Value Type

Types

Description

Range

Default value

Bool

Boolean value

True or False

FALSE

Byte

8-bit unsigned integer

0 to 255

0

Char

16-bit Unicode character

U + 0000 to U + ffff

‘0’

Decimal

128-bit exact decimal value, 28-29 significant digits

(- 7.9 x 1028 to 1028) / 100 to 28

0.0M

Double

64-bit double precision floating point type

(+ / -) 5.0x 10-324 to (+ / -) 1.7x 10308

0.0D

Float

32-bit single precision floating point type

-3.4x 1038 to + 3.4x 1038

0.0F

Int

32-bit signed integer type

-2147483648 to 2147483647

0

Long

64-bit signed integer type

-9223372036854775808 to 9223372036854775807

0L

Sbyte

8-bit signed integer type

-128 to 127

0

Short

16-bit signed integer type

-32768 to 32767

0

Uint

32-bit unsigned integer type

0 to 4294967295

0

Ulong

64-bit unsigned integer type

0 to 18446744073709551615

0

Ushort

16-bit unsigned integer type

0 to 65535

0

To get the exact size of a type or variable on a particular platform, you can use the sizeof method. Expression of sizeof(type) produces a storage size of an object or type stored in bytes. The following example is used to get any machine int storage size of the type:

Example

using System;
namespace DataTypeApplication
{
   class Program
   {
      static void Main(string[] args)
      {
         Console.WriteLine("Size of int: {0}", sizeof(int));
         Console.ReadLine();
      }
   }
}

When the above code is compiled and executed, it produces the following results:

Size of int: 4

Reference type

Reference types do not contain the actual data stored in the variable, but they do contain references to the variable.

In other words, they refer to a memory location. When using multiple variables, the reference type can point to a memory location. If the data in the memory location is changed by one variable, other variables will automatically reflect this change in value. The built-in reference types are: objectdynamic and string .

Object type

The Object type is the ultimate base class for all data types in the C# common type system (CTS). Object is System.Object alias for the class. So object types can be assigned values of any other type (value type, reference type, predefined type, or user-defined type). However, you need todo a type conversion before assigning a value.

When a value type is converted to an object type, it is called boxing; on the other hand, when an object type is converted to a value type, it is called unboxing.

object obj;
obj = 100; // This is packing

Dynamic type

You can store values of any type in dynamic data type variables. The type checking of these variables occurs at run time.

The syntax for declaring dynamic types:

dynamic <variable_name> = value;

For example:

dynamic d = 20;

Dynamic types are similar to object types, but type checking for object type variables occurs at compile time, while type checking for dynamic type variables occurs at run time.

String type

String type allows you to assign any string value to a variable. The string type is an alias for the System.String class. It is derived from the Object type. Values of type String can be assigned in two forms: quotation marks and @ quotation marks.

For example:

String str = "runoob.com";

An @ quotation mark string:

@"runoob.com";

The C# string string can be preceded by @ (called “verbatim string”) to treat the escape character () as a normal character, such as:

string str = @"C:\Windows";

Equivalent to:

string str = "C:\\Windows";

@ can wrap any line in a string, and newline characters and indented spaces are counted within the length of the string.

string str = @"<script type=""text/javascript"">
    <!--
    -->
</script>";

User-defined reference types are: classinterface or delegate . We will discuss these types in later chapters.

Pointer type

Pointer type variables store another type of memory address. Pointers in C# have the same function as pointers in C or C++.

Syntax for declaring pointer types:

type* identifier;

For example:

char* cptr;
int* iptr;

We will discuss pointer types in the section “unsafe Code”.

Powered by TorCMS (https://github.com/bukun/TorCMS).