A variable is a container used to store data. You can say that a variable is a memory location where data is stored. Suppose there are students with different scores; their scores can be put in a variable.
using System; public class HelloWorld { static void Main() { string name = "Hello World"; Console.WriteLine(name); } }
At this point, the variable name has been declared.
We have a name
, and we write it out and print it out. Take a look at another example:
using System; public class HelloWorld { static void Main() { int Age = 20; Console.WriteLine(Age); } }
We can declare multiple variables:
using System; public class HelloWorld { static void Main() { int x = 50, z = 100; Console.WriteLine(x); Console.WriteLine(z); } }
We can declare a variable in the first line and then initialize by assigning a value in the following line. We will declare a variable first:
using System; public class HelloWorld { static void Main() { int Age; Age = 36; Console.WriteLine(Age); } }
A constant variable is a variable whose value cannot be changed during program execution. In a constant variable, we cannot assign values at runtime; instead, it must be allocated at compile time.
class HelloWorld { static void Main() { const int b = 20; System.Console.WriteLine(b); } }
A variable can be of a
using System; public class HelloWorld { static void Main() { char a = 'c'; double temp = 10.68; bool b = false; string word = "Hello"; Console.WriteLine(a); Console.WriteLine(temp); Console.WriteLine(b); Console.WriteLine(word); } }
There are two categories of data types:
A value type is a data type that stores data within its memory allocation. A reference type is a data type that contains a pointer to another memory allocation that stores the data. Value type variables are stored on the stack, and reference type variables are stored on the heap.
RELATED TAGS
CONTRIBUTOR
View all Courses