C# Data Types

One of the last things we should read about are C# data types. Luckily this should be very brief and pretty easy to learn. You can think of data types as nothing more than… Er.. types of data. C# is what we call a strongly-typed programming language. This basically means is that in order create data we must predefine what type of data is it. Whether it’s numerical data, text data or character data, we must tell the compiler before hand. So lets start learning the data types we have available in C#.

 

Basic Data Types

Char

  • Character data type that represents a single Unicode character
  • A, B, C ,D, @, %, $ are some examples
  • These are enclosed with single quotation marks ‘A’

String

  • Text data type that represents a sequences of characters
  • “Hello, World!” and an example
  • String are enclosed with double quotation marks “Hello”

Int

  • Numerical data type that represents a 32 bit signed integer
  • An example would be 5, 20, 125 or 2544

Double

  • Numerical decimal data type
  • An example would be 1.5, 3.4 or 10.3

Bool

  • Logical Boolean data type
  • It can only have the values true or false

If you would like to see all data types, visit this link.

 

Comments

This is NOT a data type, but I figured I would mention them in this section.

  • Comments are ways of leaving human readable comments in your code
  • They are initiated with two forward slashes
  • Comments are ignored by the compiler
  • //This is a comment

 

Variables

You can think of variables like a single car garage. It is simply something that holds a data type and is given a name for to later be accessed or modified. They get the name that they do because it literally defines them. They vary. The value inside of them can be changed or removed at anytime and the only thing that cannot change is their data type.

Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {

        static void Main(string[] args)
        {
            //Creating a string variable with a start value
            string message = "Hello, World!";
            //Write the message variable to the console
            Console.WriteLine(message);
            //Change the message variables value
            message = "Goodbye, World!"; //Notice I did not declare string this time. The variable has already been instantiated.
            //Write the new value to the console
            Console.WriteLine(message);
            //Wait for keyboard input before closing the program
            Console.ReadLine();
        }
    }
}

First I created a string and named it message. Right off the bat I gave this message variable a value of “Hello, World!”. Next you will notice I used the Console.WriteLine like we did in in our HelloWorld project. You would also probably notice that this time I didn’t pass it a string directly, but rather passed it a variable that was a string data type. I then set our message variable a new value of “Goodbye, World!” and again, called the Console.WriteLine and passed the variable with its new value.

 

C# Hello/Goodbye World
Output from a Hello & Goodbye World console application.

 

Feel free to go ahead and copy that code in a console application to see it in action yourself and even poke around with it and make some discoveries on your own.

 

Move on to the next page >>

<<Go back to previous page

Leave a Reply

Your email address will not be published. Required fields are marked *