Variables
Published Nov 3, 2022
Contribute to Docs
A variable is a storage location in the computer’s memory that is used to save, retrieve, and manipulate data.
Syntax
Minimally, a variable is declared by specifying a data type and a name:
type name;
In this case, the variable will be initialized with the default value for its type: zero for numeric types, and null
for reference types.
A variable can also be initialized with a value when it is declared:
type name = value;
In this case, the variable name
will be set to the value value
.
Note:
value
must be of typetype
or be able to be implicitly converted totype
.
Example
using System;public class Example{public static void Main(string[] args){int x = 1;int y = x + 5;long z = y;Console.WriteLine("The int {0} can be implicitly converted to the long {1}.", y, z);// Output: "The int 6 can be implicitly converted to the long 6."}}
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.