C # Learning Log 1 (constants and variables)

Source: Internet
Author: User
Tags constant definition

 

Constant definition: The number of values that can be determined during compilation and the value does not change during the program running. Defining constant types include

Int, bool, char, double, sting, etc.

Syntax: const type name constant name = constant expression;

Example: const string s = "some text ";

Constant initialization: a constant must be initialized during definition.

Const int I = 10;

Const int j = I + 2;

Variable definition: an object with a variable name and value. (Data in a memory area)

Variable Declaration: [access modifier] type variable name [= initial value] [variable name = [initial value]…];

* The variable name must start with a letter or underscore and cannot contain special characters.

* In a variable declaration, multiple variables of the same type can be declared: int int1, int2, int3 = 3, int4;

Variable scope:

There are two methods (in vitro) Where variables are declared (in method body ). Declaring a variable in a class is sometimes called a field or a member variable.

Public does not restrict access.

Protected is limited to the current class and the derived class.

Internal access is limited to the current Assembly.

Protected internal access is limited to the current Assembly or classes derived from the current.

Private is limited to the current class.

* The variables declared in the method body cannot be modified using access modifiers such as public private.

Static variables and instance variables: static variables belong to the class and instance variables belong to the class.

Static: Class Name. variable name

Instance: Instance name. variable name

Arithmetic Operator:

++,-(Put it on the right side of the operand, and the current operand + 1/-1; on the left side, perform operations on the operand + 1-1 ).

Logical operators:

&, |, (& | Used to connect two boolean expressions )! (Unary operator); (and, or, not ):

& Indicates that when both operands are true, & returns true and else false.

| When both operands are false | returns false and else false for the operator.

! Returns false and else true if the operand is true.

Relational operators:

= ,! =,> =... Returns a Boolean value to determine whether the two operands meet a certain relationship.

Assignment operator:

+ =,-=, * =,/=

Example: a + = B is equal to a = a + B.

* The string operator only uses "+". The calculation result is to connect two strings to obtain the new string.

Conditional operators:

? : And ??

? : Is a ternary operator. Syntax: operand 1? Operand 2: operand 3

? : Evaluate operand 1 first. If the operand is true, the expression returns the value of operand 2; otherwise, the value of operand 3 is returned.

Example:

Int I = 10;

Int j;

J = (I> = 5? 100: 200); // The j value is 100.

?? : Is a binary operator. Syntax: operand 1 ?? Operand 2

?? : If operand 1 is not null, return the value of operand 1; otherwise, return the value of operand 2.

Example: www.2cto.com

String s1 = null;

String s2 = s1 ?? "Test"; // The expression returns Test

Forced type conversion:

Syntax: (type name) variable to be converted

Example:

Double doubleValue = 122.345;

Int intValue = (int) doubleValue; // convert double type data to int type data.

Enumeration: An Enumeration type is a unique type consisting of a group of naming constants.

Syntax: enum Enumeration type name [: basic type] {name constant 1 [= value] [, name constant 2 [= value]…]}

Example: enum enumSample: long {e1 = 0, e2 = 100, e3 = 300} // define the enumeration type and specify the basic type.

Enum Week {Sat, Sun, Mon, Tue, Wed, Thu, Fri}; // defines an enumeration type.

 

 

From Shine's holy heaven-Min Chen 〃

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.