Home  >  Article  >  Backend Development  >  Introduction to C# Basics-Introduction to Variables

Introduction to C# Basics-Introduction to Variables

黄舟
黄舟Original
2017-03-17 11:24:081504browse

This article mainly introduces the basic knowledge of variables in C# and has a good reference value. Let’s take a look with the editor below

Using variables is divided into 3 steps: declaration, assignment, and use.

The syntax for declaring variables: data type variable name;

The syntax for assigning values ​​to variables: variable name=value;


namespace Test
{
 class Program
 {
  static void Main(string[] args)
  {
   int age;//声明变量,类型为int,变量名为age
   age = 18;//给变量age赋值为18
   Console.WriteLine(“我今年”+age=“岁”);//打印age
   age = 19;//修改了变量age的值
   Console.WriteLine(“我明年”+age+“岁”);//打印age
  }
 }
}

The running result is:

The declaration and assignment of variables can be completed at the same time, in the Main() method of the above code The first two lines can be rewritten as:

int age=18;//Declaration and assignment

Exercise questions:


static void Main(string[] args)
{
 int age;//年龄
 age = 18;
 age = 20;
 age = 20 + 1;
 Console.WriteLine(“我今年{0}岁”,age);
 age = age - 2;
}

What this code prints is: I am 21 years old this year

The above is the detailed content of Introduction to C# Basics-Introduction to Variables. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn