This example demonstrates variable declaration and initialization in C# using common data types such as int, float, double, decimal, char, string, and bool. It also demonstrates three ways to display variable values: concatenation, string interpolation, and String.Format.
10 AM Class
A variable stores a value of a particular data type. In C#, a variable can be declared with its data type and initialized with a value in the same statement, as demonstrated in the example.
The int variable a is initialized with the integer value 100.
The float variable b is initialized with 12.23f. The f suffix indicates a float literal.
The double variable c is initialized with 23.45.
The decimal variable d is initialized with 34.56m. The m suffix indicates a decimal literal.
The char variable e is initialized with the character 'A'.
The string variable f is initialized with the text Hello, World!.
The bool variable g is initialized with the Boolean value true.
The example uses the + operator to combine text with variable values, such as "a value is :" + a.
The example uses the $ prefix and expressions inside braces, such as $"a value is : {a}".
The example uses placeholders such as {0} and passes the corresponding variables as arguments to Console.WriteLine().
> "a value is :100"> "b value is :12.23"> "c value is :23.45"> "d value is :34.56"> "e value is :A"> "f value is :Hello, World!"> "g value is :True"> ""> "a value is : 100"> "b value is : 12.23"> "c value is : 23.45"> "d value is : 34.56"> "e value is : A"> "f value is : Hello, World!"> "g value is : True"> ""> "a value is : 100"> "b value is : 12.23"> "c value is : 23.45"> "d value is : 34.56"> "e value is : A"> "f value is : Hello, World!"> "g value is : True"> ""> "a value is : 100"> "b value is : 12.23"> "c value is : 23.45"> "d value is : 34.56"> "e value is : A"> "f value is : Hello, World!"> "g value is : True"
The program imports System, System.Collections.Generic, and System.Text.
The Class3 class is placed inside the ConApp1 namespace.
Seven variables are declared and initialized using int, float, double, decimal, char, string, and bool.
The + operator combines each descriptive string with its corresponding variable value.
The $ prefix enables expressions such as {a} to be embedded directly inside the string.
Numbered placeholders such as {0} are used and the corresponding variable is supplied as an argument.
The final Console.WriteLine uses placeholders from {0} through {6} and supplies all seven variables in the same order.
An application may declare variables such as employee ID, salary, department, employee name, and active status, then display those values using concatenation, interpolation, or formatted strings.
Variable declaration specifies the data type and name of a variable.
Variable initialization assigns an initial value to a variable.
int, float, double, decimal, char, string, and bool.
The f suffix identifies the numeric literal as a float literal.
The m suffix identifies the numeric literal as a decimal literal.
String concatenation combines strings and values, using the + operator in this example.
String interpolation allows expressions to be embedded inside a string using the $ prefix and braces.
It uses numbered placeholders such as {0} and supplies the corresponding variables as arguments.
The bool variable g is initialized with true.
1. Which data type is used for variable a?
2. Which suffix is used for the float literal 12.23?
3. Which suffix is used for the decimal literal 34.56?
4. Which variable stores the character A?
5. Which variable stores Hello, World!?
6. Which variable is of type bool?
7. Which operator is used for concatenation in the example?
8. Which syntax represents string interpolation?
Declare variables using int, float, double, decimal, char, string, and bool. Initialize them with suitable values and display them using concatenation, string interpolation, and String.Format.
