Variables in C#

Tram Ho

INTRODUCTIONS

In the article BASIC EXPORT IN C # we encounter a rather strange statement that is int a = 5. This is a variable declaration and we will learn more about variables in today’s lesson – VARIABLE IN C# .

CONTENT

To best read this article you should have a basic knowledge of the sections:

  • C# command structure written on Console Application platform.
  • C# import and export structure on Console Application platform. In this lesson, we will explore the following issues:
  • What is a variable? Why use variables?
  • Declare and use variables.
  • Variable naming rules.

What is a variable? Why use variables?

In mathematics we are all too familiar with the term “variable”. If the variable in mathematics is a mutable number, it is defined in the same way in programming:

  • Is a mutable data value.
  • A name that refers to a certain memory area.
  • The core component of a programming language.

Why use variables?

Data storage and reuse: Imagine, for example, if you ask people to enter their age, but you don’t save it, until you want to use it, you don’t know where to go.

Manipulate memory easily:

  • The structure of memory consists of consecutive cells, each cell has its own address (cell address is usually hex code (hexadecimal)).
  • When you want to use any cells (allocate, destroy, get a value,..) You must pass their address. This makes programming more difficult.
  • Instead, you can declare a variable and give it a reference to the cell you need to manage and then when you use it, you will use the variable name you set, not the address of that cell anymore. Very convenient is not it!

Declare and use variables

**Syntax: **

Inside:

  • <Data type> can be:
    • Basic data type.
    • Structured data type,. . . (Will be detailed in the article DATA TYPE IN C #).
  • <Variable name>
    • The name given by the user.
    • Must adhere to the naming rules (will be discussed shortly).

Use variables

Declare:

int BienKieuSoNguyen;

float BienKieuSoThuc;

string BienKieuChuoi;

bool BienKieuLuanLy;

char BienKieuKyTu;

Inside:

  • Data types are: int, float, string, bool, char
  • Variable names are: BienKieuSoNguyen, BienKieuSoThuc, …

Use:

To use a variable we need to assign a value to it first. There are 2 ways to assign values:

  • Initialize value at declaration:

  • Assign values ​​according to the syntax:

<Variable name> = <Value>;

When you want to call a variable to get a value, you just need to call its name. For example:

or

Variable naming rules

Some rules for naming variables as well as other identifiers:

  • The variable name is a linked string (no spaces) and contains no special characters.
  • Variable names cannot be set in accented Vietnamese.
  • Names cannot begin with a number.
  • Variable names cannot overlap.
  • The variable name must not match the keyword:

Below is a list of keywords in C #, you just need to know to avoid naming the same keywords and the meaning of keywords will be presented throughout the future lessons.

In addition, the programmer also gives some general rules for naming to make it easy to manage and make it easier for others to read their code.

Camel Rules

Lower the first word and capitalize the first letter of the next word.

Commonly used to name variables private or protected (the access scope will be detailed in CLASS IN C # OBJECTIVE PROGRAMMING) and function parameters (function parameters will be detailed in the article BASIC STRUCTURE IN C #).

Examples: educationFree, vibloSite,. . .

Pascal’s rule

Capitalize the first letter of each word.

Commonly used to name the rest of the elements such as functions (will be covered in the article BASIC STRUCTURE IN C #), Interface (will be covered in INTERFACE in C # Object Oriented Programming), Enum ( will be covered in the article ENUM IN C #), Events (will be covered in the article EVENT IN C # ADVANCED),. . .

Some notes when naming variables:

Should be short and easy to understand, clearly demonstrating the purpose of the variable. For example: Name, Tuoi, GioiTinh,. . . The variable name should not be named with a character like i, k, m,. . . So later on when reviewing the code or giving the code to others to read, we will not understand what this variable is used for. Except for special cases (will be discussed in the following lessons). C # is case sensitive. For example variable a is different from variable A or Console.WriteLine () is different from Console.WRITELINE ().

These things are common standards of all programmers, if you do it differently, it doesn’t matter, but when your program crashes, it is difficult for others to help you solve it because they don’t understand the code you write. help you!

Since this article is more theoretical, Kteam will not have any demo examples. This article’s knowledge is mainly the foundation for the following lessons, so you need to know clearly so that the lessons are not surprising.

CONCLUDE

The content of this article helps you to grasp:

  • The concept of variables and why you must use them
  • How to declare and use variables.
  • Variable naming rules. In the following lesson, we will learn a concept that is DATA TYPE IN C #.
Share the news now

Source : Viblo