2.3 Data types

Stasoz
3 min readMar 29, 2022

--

You can check this course on my website.

Data types have a special meaning in C#, because it’s a strongly typed language. This means that all operations are subject to strict type checking by the compiler. Therefore, strict control of types allows to avoid errors and increase reliability of program. Moreover, the type of value defines the operations that are allowed to be performed on it. An operation allowed for one type of data may not be valid for another.

The C# language has the following primitive data types and each data type represents by system type (CLR Class):

  • bool — System.Boolean. Stores true or false. Size in memory — 1 byte.
  • byte — System.Byte. Stores whole numbers from 0 to 255. Size in memory — 1 byte.
  • sbyte — System.SByte. Stores whole numbers from -128 to 127. Size in memory — 1 byte.
  • short — System.Int16. Stores whole numbers from -32768 to 32767. Size in memory — 1 byte.
  • ushort — System.UInt16. Stores whole numbers from 0 to 65535. Size in memory — 2 bytes.
  • int — System.Int32. Stores whole numbers from -2147483648 to 2147483647. Size in memory — 4 bytes.
  • uint — System.UInt32. Stores whole numbers from 0 to 4294967295. Size in memory — 4 bytes.
  • long — System.Int64. Stores whole numbers from –9223372036854775808 to 9223372036854775807. Size in memory — 8 bytes.
  • ulong — System.UInt64. Stores whole numbers from 0 to 18446744073709551615. Size in memory — 8 bytes.
  • float — System.Single. Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits. Size in memory — 8 bytes.
  • double — System.Double. Stores fractional numbers. Sufficient for storing 15 decimal digits. Size in memory — 8 bytes.
  • decimal — System.Decimal. Stores a decimal fractional numbers. Sufficient for storing 28 decimal digits. Size in memory — 16 bytes.
  • char — System.Char. Stores a single Unicode character, surrounded by single quotes. Size in memory — 2 bytes.
  • string — System.String. Stores a string, surrounded by double quotes. Size in memory — 2 bytes per character.
  • DateTime — System.DateTime. Stores date and time. Size in memory — 8 bytes.
  • object — System.Object. Base type of all other types and can stores an any type. Size in memory — 4 bytes on a 32-bit platform and 8 bytes on a 64-bit platform.

Suffixes

When assigning values, keep in mind the following subtlety: all fractional numbers are treated as double values by default. To indicate that a fractional number represents a float type or a decimal type, you need to add a suffix to the value:

  • F / f — float;
  • M / m — decimal;

I showed it in the examples above. The same goes for integers, they are treated as int.

  • U/u — uint;
  • L/l — long;
  • UL/ ul — ulong;

Implicit typing

C# also has implicit typing, it was introduced in version 3 of the language. There’s a keyword for that — var

Depending on the assigned value the compiler will determine the type. But there are some limitations for such variables:

  • variable must be initialized;
  • variable cannot store null, because compiler cannot infer the data type;

Contents

Previous article -> 2.2 Variables

Next article -> 2.4 Type Conversion

Join to our community in Telegram — https://t.me/itifico

If you want to support me, you can buy me a cup of coffee and I will drink it when I write the next article :)

Donations list:

  1. Dinesh Chintalapudi — 3$
  2. Unknown — 5$
  3. Neisy — 3$

--

--

Stasoz
Stasoz

Written by Stasoz

Full Stack Developer who is inspired by new technologies

No responses yet