1How Does C# Store Information?
Hi friends, in this article we will learn about variables, data types and constants in C#. Whenever an application stores a name, age, price or login status, it needs a place to keep that information. A variable is a named storage location. A data type defines what kind of value it can hold. A constant stores a value that cannot change. We use them in almost every C# application.
2Think About a Shopping App
Imagine you are building an online shopping application. You need to store a customer's name, the number of items in the cart, the total bill and whether payment is completed.
The customer's name is text. The item count is a whole number. The bill can contain decimal values. Payment status is either true or false.
Each value needs a suitable data type. The application can then use these values to calculate totals and display information.
3Understand These C# Building Blocks
1. Variables
A variable has a name and stores a value. You can change its value while the program runs.
int age = 30;
age = 31;
2. Common data types
| Type | Stores | Sample |
|---|---|---|
| int | Whole numbers | 25 |
| double | Decimal numbers | 12.5 |
| decimal | Precise decimal values, useful for money | 499.99m |
| string | Text | "Chakra" |
| char | One character | 'A' |
| bool | True or false | true |
3. The var keyword
var lets the compiler identify the type from the assigned value. The type is still fixed after it is determined.
var city = "Bengaluru";
4. Constants
Use const when a value must never change after declaration. A constant needs a value when you declare it.
const double TaxRate = 0.18;
totalAmount and customerName.4Let's Build a Shopping Summary
Now let's declare different variables and a constant. We will calculate the final amount and display the shopping details.
using System;
class Program
{
static void Main()
{
string customerName = "Ravi";
int itemCount = 3;
decimal totalAmount = 1500.50m;
bool isPaid = true;
const decimal taxRate = 0.18m;
decimal tax = totalAmount * taxRate;
decimal finalAmount = totalAmount + tax;
Console.WriteLine("Customer: " + customerName);
Console.WriteLine("Items: " + itemCount);
Console.WriteLine("Paid: " + isPaid);
Console.WriteLine("Final Amount: " + finalAmount);
}
}
Here, taxRate is constant. The other variables hold values used in the calculation and display.
5What Will the Program Display?
The tax is 18% of 1500.50, which is 270.09. Adding it to the original amount gives 1770.5900 when displayed as a decimal value.
Customer: Ravi Items: 3 Paid: True Final Amount: 1770.5900
6Watch Variables Change in VS Code
Follow how the program declares values, calculates tax and displays the final amount.
7Keep These Points in Mind
Variables store values that can change. Data types define what those values can be. Constants protect values that should remain fixed.
- Variable: A named place to store data.
- Data type: Defines the kind of data.
- Constant: A value that cannot be reassigned.
Choose the correct type and meaningful names when writing C# applications.
8Questions Interviewers Love to Ask
What is a variable in C#?
AnswerA variable is a named storage location used to hold a value. Its value can change while the program runs.
What is the difference between int and decimal?
Answerint stores whole numbers. decimal stores precise decimal values and is commonly used for financial calculations.
What is the difference between var and an explicit type?
AnswerWith an explicit type, you specify the data type. With var, the compiler infers it from the assigned value. Both are statically typed.
Can we change a const variable after declaration?
AnswerNo. A const value must be assigned at declaration and cannot be reassigned later.
What is the difference between const and readonly?
Answerconst values are compile-time constants. A readonly field can be assigned at declaration or inside a constructor, so its value can be determined at runtime.