C # Basic Concepts

Source: Internet
Author: User

1. Keywords
Keywords are often used in C # code. keywords are also called reserved words. They are strings with specific meanings for C. Keywords are displayed in blue by default in the Code view of the Visual Studio environment. For example, the using, namespace, class, static, and void in the Code are all C # keywords.

2. namespace
The System namespace is imported using the using Keyword. The System is Visual Studio. the most basic namespace in. NET. when a project is created, the Visual Studio platform automatically generates and imports the namespace, which is placed at the beginning of the program code.

3. Classes and Methods
C # Each program must have only one "Main" method. The "Main" method must be placed in a class. The "Main" method is the entry to the application.

4. Statements
The statement in C # must end with a semicolon. You can write multiple statements in one row or multiple statements.

5. Braces
In C #, brackets "{" and "}" are a range sign and a way to organize code, identifies the beginning and end of a piece of code logically closely related to an application.
Braces can be nested to indicate different layers in the application.

Http://www.cnblogs.com/roucheng/

C # program format:
1. indent and Space
Indentation is used to represent the structural hierarchy of the code, but indentation can clearly represent the structural hierarchy of the program. In the program design, the code should be written in a uniform indent format.
There are two types of spaces. One is the syntax requirement, which must be followed. The other is to make the statement not too crowded. For example:
Int I = 4;

2. uppercase/lowercase letters
Uppercase "A" and lowercase "a" are two different characters for C.

3. Notes
A single line comment starts with a double slash "//" and cannot start with a line break. Multi-line comments start with "/*" and end with "*/". line breaks are allowed.

Basic Data Type:
1. Integer type
Signed integers include sbyte, short, int, and long ).
Unsigned integers include byte, ushort, uint, and ulong ).

2. Real Number Type
The real number types include float (single-precision floating point type), double (double-precision floating point type), and decimal (decimal type ).

Character Type:
1. Unicode Character Set
C # supports the Unicode Character Set.

2. char (character type)
Char (character type): The data range is 0 ~ A single character in the Unicode Character Set between 65535, occupying 2 bytes.
Char (character type) indicates a 16-bit unsigned integer. The possible value set of char (character type) corresponds to the Unicode Character Set.

3. string (string type)
String: it refers to a sequence of Unicode characters of any length. The Bytes occupied are determined by the number of characters.
A string is a string that contains numbers and spaces. It can contain only one character string or even an empty string that does not contain any character.

Boolean and Object Types
Bool (Boolean): Boolean logical volume. Bool (Boolean) data ranges from true to false ). Boolean occupies one byte. The value "true" and "false" of bool (Boolean) are keywords.
Object: it can represent any type of value, and its bytes are determined by the specific data type.
Object is the final base class of all other types. Each type in C # is derived directly or indirectly from the object type.

Variables and constants:
1. Variables
2. Declare Variables
The simplest format for declaring variables is:
Data Type name variable name list;
For example:
Int number; // declare an integer variable
Bool open; // declare a Boolean variable
Decimal bankBlance; // declare a decimal variable
Multiple variables can be declared at a time, for example:
Sbyte a, B; // declare two signed byte Variables
If multiple variables are declared at a time, variable names are separated by commas.

3. Variable assignment
C # specifies that a variable must be assigned a value before it can be referenced. To assign a value to a variable, use the value "= ". For example:
Int number;
Number = 32; // assign a value to the variable 32
You can also assign values to variables, for example:
Bool close;
Close = open; // assign a true value to the variable (assuming that open is a declared bool variable, its value is true)
You can assign values to several variables, for example:
Int a, B, c;
A = B = c = 32;
You can assign values to variables while declaring them. This is equivalent to combining the declaration statement with the assignment statement. For example:
Double area, radius = 16;

1. Direct Constants
(1) Integer constants
An integer constant is an integer. An integer constant has three forms:
In decimal format, that is, an integer in the general sense, such as 123,48910.
In the octal form, enter a constant of the octal type and add "0" before the number, for example, 0123,038.
In hexadecimal format, enter a hexadecimal integer constant, and add "0x" or "0X" before the number, for example, 0x123, 0X48910.
(2) real constants
A real-type constant is a numeric value with decimal digits. A real-type constant has two forms:
Decimal form, that is, the common writing forms of people, such as 0.123, 12.3,. 123, and so on.
The exponential form, also known as scientific notation, is composed of an E-Plus index written with an increase in the base number or a lower-case e-Plus index. For example, 123e5 or 123E5 both indicate 123 × 105.

(3) character constants
A character constant represents one character in a single Unicode character set. It generally includes numbers, letters, punctuation marks, symbols, and Chinese characters.
Character constants are defined by A pair of single quotes, such as 'A', 'A', '+', and 'hangzhou.
In C #, some characters cannot be directly placed in single quotes as character constants. In this case, escape characters must be used to represent these character constants. escape characters are composed of Backslash "/" and character, for example, '/N '.

(4) string constants
A String constant is a character sequence defined by a pair of double quotation marks. For example:
"Welcome to C #! "
"I am a student ."
It should be noted that, even if A character defined by double quotation marks is A String constant, it cannot be treated as A character constant. For example, 'A' and "A", the former is A character constant, the latter is a String constant.

(5) Boolean Constants
A boolean constant is a Boolean value. As mentioned above, true and false are two keywords of C.

2. Symbolic Constants
Symbol constants are defined using the const keyword in the format:
Const type name constant name = constant expression;

Type conversion:
1. implicit conversion
Implicit conversion is the data type conversion automatically executed by the system. The basic principle of implicit conversion is to allow conversions from a type with a small value range to a type with a large value range, and allow conversion from an unsigned integer type to a signed integer type.
2. explicit conversion
Explicit conversions are also called forced conversions. Explicit conversions explicitly indicate the conversion of a type of data to another type in the code. The general format of explicit conversion is:
(Data Type name) data
For example:
Int x = 600; short z = (short) x;
Explicit conversions may cause data loss, for example:
Decimal d = 234.55 M; int x = (int) d;


Use methods to convert data types
(1) Parse Method
The Parse method can convert a string in a specific format to a numeric value. The format of the Parse method is as follows:
Value Type name. Parse (string expression)
Example: int x = int. Parse ("123 ");
(2) ToString Method
The ToString method can convert variable values of other data types to string types. The format of ToString is as follows:
Variable name. ToString ()
For example: int x = 123; string s = x. ToString ();


Operators and expressions
① Unary operators:-(take negative), + (take positive), ++ (increment), and -- (reduce ).
② Binary operators: + (plus),-(minus), * (multiplication),/(Division), and % (remainder ).


2. String operators and string expressions
There is only one string operator, that is, the "+" operator, indicating that the two strings are connected. For example:
String connec = "abcd" + "ef ";
// The value of connec is "abcdef"
The "+" operator can also connect character-type data with string-type data or multiple character-type data. For example:
String connec = "abcd" + 'E' + 'F ';
// The value of connec is "abcdef"


3. Relational operators and relational expressions
>,<, >=, <=, == ,! =.
The values are greater than, less than, greater than or equal to, less than or equal to, equal to, and not equal.
The Relational operators used for strings can only be equal to "=" or "! = "Operator.
4. logical operators and logical expressions
In C #, the most common logical operator is! (Not), &, | (OR ).

For example:
Bool b1 =! True; // The b1 value is false.
Bool b2 = 5> 3 & 1> 2; // The value of b2 is false.
Bool b3 = 5> 3 | 1> 2 // The value of b3 is true.

5. Conditional operators and conditional expressions
The conditional operator is the only ternary operator in C #. The conditional operator consists of the symbol "?" It is composed of ":" and performs operations by operating three operands. The general format is:
Boolean expression? Expression 1: expression 2


6. Value assignment operators and value assignment expressions
In the value assignment expression, the left operand is the left operand, And the right operand is the right operand. The left operand is usually a variable.
Compound assignment operators, such as "* =", "/=", "% =", "+ =", and "-=.

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.