0
0
C Sharp (C#)programming~5 mins

Explicit value assignment in C Sharp (C#)

Choose your learning style9 modes available
Introduction

Explicit value assignment lets you set a specific value to a variable or constant. This helps you control what data your program uses.

When you want to start a variable with a known number or text.
When you need to set a constant value that should not change.
When you want to update a variable with a new value during the program.
When you want to make your code clear by showing exact values.
When you want to assign values to properties or fields in a class.
Syntax
C Sharp (C#)
type variableName = value;

The type is the kind of data, like int for numbers or string for text.

The variableName is the name you choose for your variable.

Examples
Assigns the number 25 to the variable age of type int.
C Sharp (C#)
int age = 25;
Assigns the text "Alice" to the variable name of type string.
C Sharp (C#)
string name = "Alice";
Assigns the constant value 3.14159 to Pi. This value cannot change later.
C Sharp (C#)
const double Pi = 3.14159;
Sample Program

This program shows how to assign values explicitly to variables and a constant. It prints the values, then updates the score and prints it again.

C Sharp (C#)
using System;

class Program
{
    static void Main()
    {
        int score = 100;
        string player = "Bob";
        const int maxScore = 1000;

        Console.WriteLine($"Player: {player}");
        Console.WriteLine($"Score: {score}");
        Console.WriteLine($"Max Score: {maxScore}");

        // Update score
        score = 150;
        Console.WriteLine($"Updated Score: {score}");
    }
}
OutputSuccess
Important Notes

Constants use the const keyword and cannot be changed after assignment.

Variables can be updated with new values anytime after their initial assignment.

Use meaningful variable names to make your code easier to understand.

Summary

Explicit value assignment sets a clear starting value for variables or constants.

Use = to assign values in C#.

Constants cannot change once set, variables can.