C# Data Types and Variables

Published on
4 mins read
––– views

Why Data Types Matter?

In the world of programming, certain languages, like C# and Java, require developers to specify data types in advance. This isn't true for all languages, though. Take JavaScript as an example. It's quick, it's easy, and it doesn't get fussy about what type of data you throw at it.

For small apps, defining data types seems like overkill. Hit a bug? No biggie, you debug it.

But imagine dealing with a mammoth app, like bank software, decades old, worked on by numerous devs. Here, precision is key.

Setting up data types in languages like C# or Java pays off. Spot a data type mismatch? They'll yell with an error right away. This early warning system is priceless.

Contrast this with loosely typed languages. You might have a data type problem buried ten functions deep, causing a headache nine levels up. That's a debugging nightmare you don't want to deal with.

Statically Typed

C# is statically typed, meaning the computer verifies the correctness of data types prior to running the program. This acts as a frontline defense, warding off bugs before the application is launched into production.

Regardless of the proficiency of you or your team, bugs are a common occurrence in large-scale applications. Hence, it's crucial to minimize them as much as possible.

Data Types

int - Whole Numbers - any number between -2,147,483,648 to 2,147,483,648

long - whole numbers wider range -9,223,372,036 to 9,223,372,036

int costs 4 bytes and long costs 8 bytes to the system. If long isn't needed, do not use it.

float - floating numbers like 2.2 or -1.3 and it uses 32 bits of storage. Offers about 7 digits of precision.

double - double precision floating number. Uses 64 bits of storage and offers about 15-16 digits of precision.

decimal - 128-bit data type and it's more precise then float and double

Do not use double or decimal if required level of precision can be achieved with float. This will help you save some memory.

char - saves single character, like 'A', 'B' and so on.

string - saves multiple characters, "this is a string".

You must use double quotes " " for strings, and single quotes ' ' for char.

bool - Boolean is used for True or False.

DateTime used for dates.

Declaring Variables

First, we need to decide what type of data we are going to use. Is it a number? if yes, is it a whole number or not?

When people tell their age, they usually use whole numbers. I haven't heard anyone saying I'm 22.3 years old. So, age is an integer.

int age;
age =20;

If someone asks your favorite color, it's going to be a word. Because the number 8 cannot be your favorite color.

string favColor = "green"

Do not forget to wrap strings between " " otherwise, you will get an error. int is a number and " " isn't required.

using System;

namespace Form
{
  class Program
  {
    static void Main(string[] args)
    {
      // Create Variables
int age = 20;
string color = "green";

      // Print variables to the console

Console.WriteLine($"I'm {age} years old and my fav color is {color}");

    }
  }
}

This will output I'm 20 years old and my fav color is green.

Converting String to Int

Let's say we need the user input,

Console.Write('type a number');

When the user enters the value, computer will take it as a string not an integer, int.

We can convert it to int with Convert.ToInt32()

Console.Write('type a number');

int number = Convert.ToInt32(Console.ReadLine());

Second line will convert the user input from a string to int. We need to place Console.ReadLine() inside the build in C# function Convert.ToInt32().