Loading...

Variables, Data Types and User Input in C#

Think about how your phone saves your name, your age, your email, or your favorite settings.
These pieces of information are stored somewhere, not permanently in your memory, but inside small storage boxes the device creates on demand.

In programming, these storage boxes are called variables. Just like you store your personal details, your programs also need a way to remember things while they run.
Today, you’ll teach C# how to remember.

By the end of this lesson, you’ll build your own About Me Profile Card using variables and input.

The Project: About Me Profile Card

Your program will ask the user for:

  • Their name
  • Their age
  • Their favorite hobby

and print a neat profile card like this:

text
6 lines
|
28/ 500 tokens
1
2
3
4
5
6
========================
   ABOUT ME PROFILE
   Name: Ali
   Age: 15
   Hobby: Football
========================

Let’s learn the pieces needed to build it.

Understanding Variables

A variable is a small storage box in memory.
You give it:

  • a type (what kind of data it holds)
  • a name (your label)
  • a value (the information stored inside)

Example:

csharp
2 lines
|
9/ 500 tokens
1
2
string name = "Ali";
int age = 15;
Code Tools

Here:

  • string stores text
  • int stores whole numbers

You can change the value anytime, and the program will automatically use the updated information.

Common Data Types in C#

Here are the four beginner data types you’ll use most:

  • int → whole numbers (12, 50, 1000)
  • double → decimal numbers (5.5, 2.75)
  • string → text (“Hello”, “Ali”)
  • bool → true/false values (true, false)

These four alone can help you build dozens of beginner programs.

Declaring and Updating Variables

Declaring a variable:

csharp
1 lines
|
4/ 500 tokens
1
int score = 10;
Code Tools

Updating it later:

csharp
1 lines
|
3/ 500 tokens
1
score = 20;
Code Tools

C# always uses the latest stored value.

User Input with Console.ReadLine()

To make your program interactive, use this:

csharp
1 lines
|
9/ 500 tokens
1
string name = Console.ReadLine();
Code Tools

Whatever the user types gets stored in the variable name. For numbers, you need to convert the input:

csharp
1 lines
|
12/ 500 tokens
1
int age = Convert.ToInt32(Console.ReadLine());
Code Tools

This turns text into a number so C# can understand it correctly.

Let’s Build the Profile Card

Here’s the full program:

csharp
23 lines
|
163/ 500 tokens
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("Enter your name:");
        string name = Console.ReadLine();

        Console.WriteLine("Enter your age:");
        int age = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Enter your hobby:");
        string hobby = Console.ReadLine();

        Console.WriteLine("========================");
        Console.WriteLine("   ABOUT ME PROFILE");
        Console.WriteLine($"   Name: {name}");
        Console.WriteLine($"   Age: {age}");
        Console.WriteLine($"   Hobby: {hobby}");
        Console.WriteLine("========================");
    }
}
Code Tools

Notice how $"{name}" lets us insert variables directly inside a string, this is called string interpolation, and it makes formatting much cleaner.

Run it inside the DevsCall AI Code Runner to see your personalized output.

How It Works

  • You ask for input using Console.ReadLine().
  • Store that input in variables.
  • Print everything using formatted text.
  • The program dynamically changes based on what the user types.

You’ve now made your first interactive C# app.

Learn Together with AI

Try using these prompts with your AI Copilot:

  • “Rewrite my About Me program with colors or ASCII borders.”
  • “Explain the difference between int and double in simple words.”
  • “Show me how to handle invalid age input.”
  • “Create a version that asks for favorite food too.”

AI will help you explore different versions and deepen your understanding.

Practice Time

Write a program that asks for:

  • Student Name
  • Grade
  • Favorite Subject

…and prints this:

csharp
6 lines
|
30/ 500 tokens
1
2
3
4
5
6
========================
   STUDENT INFO
   Name: Sara
   Grade: 10
   Favorite Subject: Math
========================
Code Tools

Use Console.ReadLine() to take all three inputs.

Example Solution

csharp
23 lines
|
171/ 500 tokens
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("Enter your name:");
        string name = Console.ReadLine();

        Console.WriteLine("Enter your grade:");
        int grade = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Enter your favorite subject:");
        string subject = Console.ReadLine();

        Console.WriteLine("========================");
        Console.WriteLine("   STUDENT INFO");
        Console.WriteLine($"   Name: {name}");
        Console.WriteLine($"   Grade: {grade}");
        Console.WriteLine($"   Favorite Subject: {subject}");
        Console.WriteLine("========================");
    }
}
Code Tools

If your output matches, your variables and input handling are working perfectly!

Frequently Asked Questions

int, double, string, and bool, these let you store whole numbers, decimals, text, and true/false values.

Use Console.ReadLine() to read text, and convert it when needed using methods like Convert.ToInt32().

Yes. Variables can be reassigned new values anytime, and the program will use the latest stored value.

C# may show an error when converting text to numbers. Later lessons teach how to handle errors safely using exceptions.

Still have questions?Contact our support team