Variables
Variables are used to store and manipulate data. In C#, each variable has a type that determines the values it can store.
Types of variables
In C#, there are five distinct types of variables:
Name | Description |
---|---|
Local variables | Variables declared within a method, constructor, or code block, accessible only within that scope. |
Instance variables | Non-static fields in a class, storing data unique to each instance of the class. |
Static Variables | Variables shared across all instances of a class, belonging to the class itself. |
Constant Variables | Immutable values initialized at the time of declaration, typically declared with const and behaving similarly to static members. |
Readonly Variables | Values that can be assigned during declaration or in a constructor but cannot be modified afterward. |
Syntax
type name = value;
type
: The data type of a variable, defining the kind of data it can hold.name
: The identifier for the variable, used to reference it in the code.value
: An optional initial value for the variable. If omitted, it must be assigned a value before use.
Examples
Local variables
In this example, localVariable
can only be used within the MyMethod()
function:
void MyMethod(){int localVariable = 10; // This variable is only accessible inside MyMethod}
Instance variables
The example demonstrates an instance variable (name
) within a class (Person
). Each instance of Person
will have its own copy of name
:
class Person{public string name; // Instance variablepublic Person(string personName){name = personName; // Initialize instance variable}}
Static Variables
Here, wheels
is a static variable, meaning it belongs to the Car
class and is shared by all instances. Note that static variables can be modified unless marked as const
:
class Car {public static int wheels = 4; // Static variable}
Constant Variables
In this example, Pi
is a constant variable, meaning its value cannot be changed after declaration and is shared across all instances:
class Circle{public const double Pi = 3.14159; // Constant variablepublic double CalculateArea(double radius){return Pi * radius * radius; // Using the constant variable}}
Readonly Variables
In this example, length
and width
are readonly variables assigned in the Rectangle
constructor. Their values cannot be changed after the instance is created:
class Rectangle{public readonly double length; // Readonly variablepublic readonly double width; // Readonly variablepublic Rectangle(double l, double w){length = l; // Assigning value in the constructorwidth = w; // Assigning value in the constructor}}
Codebyte Example
Here’s an example showcasing the use of various types of variables:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C# on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn C#
Learn Microsoft's popular C# programming language, used to make websites, mobile apps, video games, VR, and more.Beginner Friendly23 hours