C# Built in Data Types

Introduction

C# is an object-oriented and component-oriented programming language. The namespace declaration is like using System, and it indicates that you are using the System namespace. If you remove the using System declaration, then we can use the fully qualified name of the Console class. A namespace is used to arrange your code and is a collection of classes, interfaces, structs, enums, and delegates.

  • Console.WriteLine(): It writes to the console.
  • Console.ReadLine(): It reads from the console.
  • using System: The namespace declaration.
  • static void Main(string[] args): It is the entry point into your application.

Description

In this article, I will show the different built-in types that are available in C#.

  • Boolean type: Only true or false 
  • Integral Types: sbyte, byte, short, ushort, int, uint, long, ulong, char
  • Floating Types: float and double
  • Decimal Types: Decimal Values 

Boolean type

Boolean type contains Only true or false. suppose your application needs to store value as true or false. for example, there is a scenario like are you a student or not?

bool b = true; //Boolean type contains only true or false

It throws a compile time error if we assign any other values like integer or string etc.

For integer

Integer

For string

String

Integral Types

C# supports the following predefined integral types.

Integral types

For example, you want to save the age of a person, and age is relatively small, so byte is more than enough.

byte age = 123; //The age of person

If it exceeds, then it shows a compile-time error because the range is 0 to 255.

Byte

if you want to store the population of the country, then you need an integer.

int population = 123564343;

if it is more than int, then use long. so, depending on requirements, we can use the type.

How to calculate the min and max values of the integer can be stored using Visual Studio.

int i = 0;
Console.WriteLine("Min = {0}", int.MinValue); //Min value of integer
Console.WriteLine("Max = {0}", int.MaxValue); //Max value of integer

int

Floating Types

C# supports the following predefined floating-point types.

floating-point types

The literal without suffix or with the d or D suffix is of type double.

double d1 = 123.356453722; //The literal without suffix
double d2= 341.356453722d; //The literal with the d or D

The literal with the f or F suffix is of type float.

float f1 = 431.323467f; //The literal with the f or F suffix is of type float

Decimal Types

The literal with the m or M suffix is of type decimal.

decimal dc1 = 1342.276267721712m;

Summary

This article introduces C# programming basics, including data types and their usage. It covers Boolean types, integral types, floating-point types, and decimal types. C# is a versatile language used for various applications, and understanding these fundamental types is crucial for writing effective code.


Similar Articles