C # The Path to learning-core in the Foundation (I): Variables

Source: Internet
Author: User

Chen

I am a cainiao, a great cainiao. All I need to learn is to start learning from the basics. Today I am free. I will summarize what I have learned to consolidate my knowledge! I hope you can point it out!

  1. Variables

It's easy to declare variables, so I don't have to say much! Note the following when declaring a variable:

① Variables are fields in a class or structure. If Initialization is not displayed, the default value is 0 when these variables are created.

② Local variables of the method must be displayed in the Code for initialization before they can be used in the statement. At this time, Initialization is not performed when the variable is declared, but the compiler checks all possible paths through methods and how to detect that the local variable uses its value before initialization, an error occurs.

2. type inference

Type inference trial var keyword. The syntax for declaring variables has changed. The compiler can "deduce" the type of the Variable Based on the variable initialization!

Example Code:

1      Static VoidMain (String[] Args)
2 {
3 Var name= "Bugs Bunny";
4 Var age= 25;
5 Var isRabbit= True;
6
7 Type nameType=Name. GetType ();
8 Type ageType=Age. GetType ();
9 Type isRabbitType=IsRabbit. GetType ();
10
11 Console. WriteLine ("Name is type" +NameType. ToString ());
12 Console. WriteLine ("Age is type" +AgeType. ToString ());
13 Console. WriteLine ("IsRabbit is type" +IsRabbitType. ToString ());
14 //Export:
15 //Name is type System. String
16 //Age is type System. Int32
17 //IsRabbit is type System. Bool
18  }
However, four rules must be followed when declaring variables:

①. The variable must be initialized. Otherwise, the compiler will not be able to deduce the variable type.

②. The initialization device cannot be empty.

③ The initiator must be placed in the expression.

④ The initiator cannot be set as an object unless a new object is created during initialization.

3. Scope of Variables

The scope of a variable is the code area that can access the variable. In general, confirm the scope to follow the rules below:

As long as the class is in a specific scope, its fields (also called member variables) are also in this scope.

  • A local variable exists in the scope that is before the block statement or method that declares the variable ends.
  • The local variables declared in for, while, or similar statements exist in the same loop.

4. Constants

When declaring and initializing a variable, add a keyword before the variableConstYou can specify this variable as a constant.

1 const int a = 100

Constants have the following features:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.