Primary knowledge of Web front-end Learning--c# (I.)

Source: Internet
Author: User
Tags arithmetic operators type casting

I. About C # program structure

This is the first C # program of the niche, used to understand the initial understanding of the program structure enough.

The first line of the program using System; -The Using keyword is used to include the System namespace in your program. A program generally has multiple using statements.

The next line is the namespace declaration. A namespace is a series of classes. The Helloworldapplication namespace contains class HelloWorld.

The next line is the class declaration. Class HelloWorld contains the data and method declarations used by the program. Class generally contains multiple methods. Method defines the behavior of the class. Here, the HelloWorld class has only one Main method.

The next line defines the Main method, which is the entry point for all C # programs. The Main method describes what actions the class will do when it executes.

The next line of/*...*/will be ignored by the compiler, and it will add additional annotations to the program.

The Main method passes the statement Console.WriteLine ("Hello World"); Specifies the behavior of it.

WriteLine is a method that defines the Console class in the System namespace. The statement displays the message "Hello, world!" on the screen.
Last line Console.ReadLine (); is for vs.net users. This allows the program to wait for a keystroke action to prevent the screen from running and shutting down quickly when the program starts from Visual Studio. NET.


The following points are worth noting:

C # is case-sensitive.
All statements and expressions must end with a semicolon (;).
The execution of the program starts with the Main method.


Two. Identifiers ("naming" of variables)

Identifiers are used to identify a class, variable, function, or any other user-defined item. In C #, the naming of a class must follow the following basic rules:

Identifiers must begin with a letter, underscore, or @, followed by a series of letters, Numbers (0-9), underscores (_), and @.
The first character in an identifier cannot be a number.
The identifier must not contain any embedded spaces or symbols, such as? - +! #% ^ & * () [] {}. ; : " ‘ / \。
The identifier cannot be a C # keyword. Unless they have an @ prefix. For example, @if is a valid identifier, but if is not, because if is a keyword.
Identifiers must be case sensitive. Uppercase and lowercase letters are considered to be different letters.
three. keywordsThe keyword is a predefined reserved word for the C # compiler. These keywords cannot be used as identifiers, but if you want to use these keywords as identifiers, you can prefix them with the @ character before the keyword.

In C #, some keywords have special meanings in the context of the code, such as Get and set, which are called contextual keywords (contextual keywords).
Keywords reserved in C #:

Contextual keywords:

Four. Data type

1. Value type (types)

Value type variables can be assigned directly to a value. They are derived from the class System.ValueType.
Value types directly contain data. such as int, char, float, respectively, they store numbers, letters, floating point number. When you declare an int type, the system allocates memory to store the value.

2. Reference type (Reference types)

Reference types do not contain the actual data stored in variables, but they contain references to variables.

In other words, they refer to a memory location. When you use multiple variables, the reference type can point to a memory location. If the data in the memory location is changed by one variable, the other variables will automatically reflect this value change. The built-in reference types are: object, dynamic, and string. 3. Pointer type (Pointer types)
A pointer type variable stores another type of memory address. Pointers in C # have the same functionality as pointers in C or C + +. five. Type conversionType conversions are also called type casting, or the conversion of data from one type to another. There are two forms of type casting in C #:

1. Implicit conversions

Implicit conversions do not require any work, and you do not need to write code in addition. If you convert int data to double type data:

Ten;  Double b = a;/* implicit conversion *  /
The implicit conversion rule is that any type a can be implicitly converted to type B as long as its range of values is fully contained within the range of the value of type B. Based on this translation rule, implicit conversion of C # does not result in data loss. It is important to note that there is no implicit conversion of the simple type bool and string that we use most often.
2. Explicit conversion (mandatory)

Explicit conversions: Conversions from Type A to type B can only be done in some cases, the conversion rules are complex, and some kind of extra processing should be performed. Explicit conversions are also called coercion type conversions, and explicit conversions require the user to explicitly specify the type of conversion. If you convert a double type data to an int type data:

            10.5;            int d = (int) c;  /* Show conversion */

Remind:

An explicit conversion may result in an error. When this conversion is made, the compiler will overflow detect the transformation. If there is an overflow stating that the conversion failed, it indicates that the source type is not a valid target type. Type conversions are not possible.

Forcing type conversions can result in data loss, as in the above example, the resulting D value is 10.

3. Using method Conversions

(1), through Int. Parse () method conversion, parameter type only supports string type. Note: The value of string cannot be null when using this method, otherwise it cannot be converted, and the string type parameter can only be a variety of integers, cannot be floating-point, or it cannot be converted (for example, Int. Parse ("2.0") cannot be converted).

            int i;            Int. Parse ("+");

(2), use the ToString () method. All types inherit the object base class, so there is the ToString () method (which converts to a string).

(3), through Int. The TryParse () method converts the conversion method to int. The Parse () transformation method is similar to the difference between int. The Parse () method cannot be converted successfully, and the method performs normally and returns 0. meaning int. The TryParse () method is more than int. The Parse () method has more than one exception handling, returns False if an exception occurs, and returns the output parameter to 0.

            int i;            null;            Int. TryParse (s, outi);    

(4), conversion through the Convert class, the Convert class provides a lot of conversion methods. The premise of using these methods is that you can convert the objects you want to convert to the appropriate type, and if you cannot convert, you will report the wrong format. Note: When using Convert.ToInt32 (double value), if value is a number in the middle of two integers, the even numbers are returned, that is, 4.5 is converted to 4, and 5.5 is converted to 6.

(5), to achieve their own conversion, through the inheritance interface iconventible or Typeconventer class, so as to achieve their own conversion.

six. Variable (a variable is a storage unit named in memory or register with an identifier that can be used to store a specific type of data, and the value of the data can be modified while the program is running.) )

Variable definitions in the 1.c#
Syntax for variable definitions in C #:
Data type variable name 1, variable name 2, variable name 3 ...;
Here, the data type must be a valid C # data type, which can be a char, int, float, double, or other user-defined data type. Variable names can consist of one or more comma-delimited identifier names.
2. Variable initialization (Assignment): A variable can be initialized at the time of declaration (specifying an initial value). Initialization consists of an equal sign followed by a constant expression. Initialize to accept values from the user:
The Console class in the System namespace provides a function, ReadLine (), to receive input from the user and store it in a variable. Seven. OperatorsAn operator is a symbol that tells the compiler to perform a specific mathematical or logical operation. C # has a rich set of built-in operators, categorized as follows:
1. Arithmetic operators assume a=10,b=20

2. Relational operator (output "bool" value) assumes a=10,b=20


3. Logical operator (output "bool")

Assume that variable A is a Boolean value of true, and variable B is a Boolean value of False

4. Bitwise operators

5. Assignment operators


6. Other operators

7. Operator Precedence


Primary knowledge of Web front-end Learning--c# (I.)

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.