Skip to content

CSharp Basics Types Variables

Joe Care edited this page Dec 22, 2025 · 1 revision

C# Basics: Primitive Types and Variables

This lesson introduces the most common built-in types in C# and how to declare and assign variables.

Related wiki pages:

  • General overview: CSharpBible.md
  • Input/output basics: CSharp-Basics-ConsoleWriteLine-DateTime.md, Lesson20.12.2025.md (to be renamed)

Official docs:


1. Core built-in types

Some frequently used types:

  • int – 32-bit signed integer (e.g. 10, -5, 0)
  • double – double-precision floating-point (e.g. 3.14, -2.7)
  • string – text enclosed in double quotes (e.g. "Hello")
  • booltrue or false
  • char – single character in single quotes (e.g. 'A')

2. Declaring and assigning variables

Explicit typing:

int count = 10;
string greeting = "Welcome to C#!";
bool isActive = true;

Type inference with var:

var message = "Hello"; // inferred as string
var number = 42;        // inferred as int

var is still statically typed; the compiler infers the type from the right-hand side.


3. Simple console example

using System;

namespace BasicConcepts
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var greeting = "Welcome to C#!";
            Console.WriteLine(greeting);
        }
    }
}

This prints:

Welcome to C#!

For more console examples see CSharp-Basics-ConsoleWriteLine-DateTime.md and CSharpBible-Library-ConsoleLib.md.


4. Next steps

  • Try declaring variables of each primitive type and printing them.
  • Combine this with input from Console.ReadLine as shown in Lesson20.12.2025.md (to be renamed to an I/O focused title).

Clone this wiki locally