GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — C# - Variables
Selected Reading
C# - VariablesIntroduction to C# VariablesC# variables are fundamental building blocks in any C# program and are used to store data. You can assign, access, and manipulate the data with the help of variables. In this chapters, we will learn about C# variables, how to declare them, accessing, and manipulating the variables values. What Are Variables in C#?C# variables are the containers to store data and help you to access and manipulate the data during the program execution. A variable must be declared by using a specific data type which can store that type of value. Why Are Variables Important in C#?
Declaring Variables in C#In C#, you declare a variable by specifying its data type and a variable name. SyntaxHere's the basic syntax to declare a variable: <data_type> <variable_name>; Exampleint age; string name; C# Variable InitializationAfter declaring a variable, you can initialize it with a value. ExampleHere's an example of how to do this: int age = 21; string name = "Zoya"; bool isActive = true; You can also assign a value to a variable after declaration: Exampleint age; age = 25; Types of Variables in C#C# supports several types of variables, categorized as follows: 1. Primitive VariablesThe primitive variables are basic data types like Exampleint number = 10; double pi = 3.14; 2. Reference VariablesThe reference variables hold references to objects in memory, like arrays and classes. Examplestring name = "Alice";
int[] numbers = new int[] { 1, 2, 3 };
3. ConstantsThe constants are variables whose value cannot be changed once assigned. Exampleconst double PI = 3.14159; 4. Nullable VariablesThe nullable variables can hold a Exampleint? age = null; Best Practices for Using Variables in C#
Common C# Variable ExamplesHere are a few examples of variables in action to give you a better understanding: 1. Storing User InformationIn this example, we store user details such as first name, last name, and age. using System;
class Program
{
static void Main()
{
string firstName = "Sudhir";
string lastName = "Sharma";
int userAge = 28;
Console.WriteLine("User: " + firstName + " " + lastName);
Console.WriteLine("Age: " + userAge);
}
}
This example will produce the following output: User: Sudhir Sharma Age: 28 2. Performing Simple ArithmeticThis example demonstrates a basic arithmetic operation. using System;
class Program
{
static void Main()
{
int shirts = 12;
int trousers = 8;
int totalClothes = shirts + trousers;
Console.WriteLine("Total number of clothes: " + totalClothes);
}
}
This example will produce the following output: Total number of clothes: 20 3. Using Boolean VariablesThis example shows how a boolean variable can be used to control program flow. using System;
class Program
{
static void Main()
{
bool isMember = true;
if (isMember)
{
Console.WriteLine("Welcome, valued member!");
}
else
{
Console.WriteLine("Please sign up for a membership.");
}
}
}
This example will produce the following output: Welcome, valued member! Conclusion: Mastering C# VariablesMastering variables in C# is a key skill for any developer. Whether you're storing basic values or working with complex data structures, understanding how variables work helps you build a strong foundation in C#. By choosing the right data types, using clear variable names, and following best practices, you can write clean and efficient code that's easy to maintain. FAQ About C# Variables1. What is the difference between
|
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |