C# Tutorial
C# String
C# Array
C# Flow Control
C# Class and Object
C# Inheritance
C# Interface
C# Collection
C# Generic
C# File I/O
C# Delegate and Event
C# Exception
C# Process and Thread
C# ADO.NET Database Operations
In C#, variables are used to store data in a program. Variables have a data type, which defines the kind of data they can store, and a name, which is used to reference the variable in the code. In this tutorial, we'll cover the basics of working with variables in C#.
To declare a variable, you need to specify its data type followed by the variable name. The variable name should adhere to the naming rules and conventions discussed in the previous tutorial.
int age; string firstName; double interestRate;
After declaring a variable, you can assign a value to it using the assignment operator (=).
age = 25; firstName = "John"; interestRate = 3.5;
You can also declare a variable and assign a value to it in the same statement.
int age = 25; string firstName = "John"; double interestRate = 3.5;
You can use variables in expressions, such as arithmetic operations, comparisons, and method calls.
int x = 10; int y = 20; int sum = x + y; // 30 bool isAdult = age >= 18; // true Console.WriteLine("Hello, " + firstName); // "Hello, John"
C# provides several built-in data types for working with different kinds of data:
byte
, short
, int
, and long
for whole numbers.float
and double
for real numbers with decimal points.decimal
for high-precision decimal numbers, often used for financial calculations.bool
for true or false values.char
for single Unicode characters.string
for sequences of characters.byte smallNumber = 42; float piApproximation = 3.14f; decimal price = 19.99m; bool isComplete = false; char letter = 'A'; string message = "Hello, World!";
You can use the var
keyword to let the compiler infer the data type of a variable based on the assigned value. The variable still has a specific data type, but you don't need to specify it explicitly.
var age = 25; // int var firstName = "John"; // string var interestRate = 3.5; // double
In this tutorial, we covered the basics of working with variables in C#. Variables are used to store data in a program, and they have a data type and a name. You can declare a variable, assign a value to it, and use it in expressions. C# provides several built-in data types for working with different kinds of data, and you can also use the var
keyword to let the compiler infer the data type of a variable based on the assigned value.
Declaring variables in C#
Variable declaration in C# involves specifying the variable's data type followed by its name. The var
keyword can also be used for implicitly typed variables.
// Explicitly typed variable declaration int age; // Implicitly typed variable declaration var name = "John";
Data types in C# variables
C# supports various data types such as integers (int
), floating-point numbers (float
, double
), characters (char
), strings (string
), and more. Each type has a specific purpose and size in memory.
int intValue = 42; double doubleValue = 3.14; char charValue = 'A'; string stringValue = "Hello, World!";
C# variable scope
Variable scope defines where a variable is accessible in code. Local variables are confined to the block where they are declared, while global variables are accessible throughout the entire code.
void MyMethod() { int localVar = 10; // Local variable // localVar is accessible within this method }
Constant variables in C#
Constants are variables whose values cannot be changed. They are declared using the const
keyword.
const int MaxValue = 100;
Local vs. global variables in C#
Local variables are declared within a specific block or method and have limited scope. Global variables, on the other hand, are declared outside any method or block and have broader scope.
int globalVar = 50; // Global variable void MyMethod() { int localVar = 10; // Local variable }
Nullable types in C# variables
Nullable types allow variables to have a value of null
in addition to their regular data type values. They are declared using the ?
symbol.
int? nullableInt = null;
C# readonly variables
readonly
variables can only be assigned a value at the time of declaration or within the constructor of the containing class.
readonly int readOnlyVar = 100;
Static variables in C#
static
variables belong to the class rather than instances of the class. They are shared among all instances of the class.
class MyClass { static int staticVar = 42; void MyMethod() { // Access static variable int value = MyClass.staticVar; } }
C# variable initialization
Variables can be initialized at the time of declaration or later in the code. Uninitialized variables may contain garbage values.
int initializedVar = 10; int uninitializedVar;
C# dynamic variables
dynamic
variables are resolved at runtime, enabling late binding. They provide flexibility but may sacrifice type safety.
dynamic dynamicVar = "Hello";
C# variables and memory management
C# handles memory management automatically through garbage collection. Variables are allocated memory when they are declared and deallocated when they go out of scope.
void MyMethod() { int localVar = 10; // Memory for localVar is automatically managed }
Using var keyword in C# variables
The var
keyword allows for implicit typing, where the compiler determines the variable's type based on the assigned value.
var implicitVar = "Implicit type";
Scope resolution in C# variables
Scope resolution refers to determining which variable is being referenced in a given context, especially when variables have the same name in different scopes.
int globalVar = 50; void MyMethod() { int localVar = 10; int result = globalVar + localVar; // Resolving scope }