C # basic knowledge of C # BASIC program structure, C # syntax basics, etc,

Source: Internet
Author: User

C # basic knowledge of C # BASIC program structure, C # syntax basics, etc,

Program Design Overview:

The software includes two aspects: Program and document. The programming language is a specialized language used to compile computer programs, such as C #, C ++, and Java. The development of the programming language is as follows: machine-assembly-advanced.

What is C #:

It is a programming language. C # has evolved from C/C ++, java, and other languages. It is a modern, simple, and object-oriented programming language; powerful functions can be applied to the vast majority of system development and provide development efficiency beyond other programming languages.

About. Net:

Microsoft. Net provides us with a brand new Internet and software development environment. software becomes a service, and any terminal device and platform are integrated with similar Human-Computer Interaction interfaces. The. NET framework built on the CLR and. NET class libraries is one of the core components of the. NET platform.

C # BASIC program structure:

Import a namespace (using... )

Namespace... {})

Define class... {})

Main method (static void Main (){})

C # identifier rules:

It must start with a letter or underscore, followed by letters, underscores, and numbers. keywords can also be used as identifiers, but @ symbols must be added before keywords; C # strictly case sensitive (age and Age are two different identifiers)

C # BASIC program structure (namespace, class, method, block, etc.) and composition elements (statements, identifiers, white spaces, comments, punctuation marks, etc)

Escape characters in the output: \ n: line feed; \ t: Space

Computer memory is a series of containers that store values.

C # three types of memory are available: stack, managed heap, and unmanaged heap.

Data Types of variables in C:

C # language types include value type and reference type;

Value Type variables are stored in the stack, and reference type variables are stored in the managed stack.

Values: simple type, enumeration type (enum), and structure type (structs)

The following types are referenced: class type, array type, interface type, and delegate type)

C # syntax basics:

Constant:

Symbol constants must also be defined before use. Syntax: const data type constant name = constant value;

Operator type:

Arithmetic Operators: Single Object +,-binocular: +,-, *,/, and %.

Self-OPERATOR: ++, -- (prefix is used before self-calculation, suffix is used before self-calculation)

Relational operators: >,<, >=, <=, =, and ,! =

Assignment operators: =, + =,-=, * =,/=, and % =.

Logical OPERATOR: single object (non-logical !) Binary (logical and &, logical or |)

Conditional OPERATOR: B? X: y, also known as the ternary operator, is a unique ternary operator. It requires three operands. Syntax format: <relational expression 1>? <Expression 2>: <expression 3>

*: General operators in C #: logical operators, conditional operators, etc.

Data type conversion:

Implicit conversion: Generally, values of the lower type are converted to the higher type. (except for a few special to floating-point conversions, the accuracy may be lost, but the data will not be lost.) This ensures that the values do not change, relatively safe.

Display conversion: An error may occur during display conversion, which is generally caused by overflow. To ensure successful conversion, the value of high data must be within the maximum range of low data values. Otherwise, an exception occurs.

Several methods of forced conversion:

Use functions in the Convert class

Convert the string to any type using the Parse function of each type

Convert any type into a string using the ToString function of each type

You can also directly add parentheses in front of the variable to specify the target type for conversion,

A statement is the basic unit for the program to complete a complete operation. C # statement:

Empty statement, with only one semicolon and declaration statement: declaring variables or constants

Expression statement, composed of an expression

Method call statement

Mark statement

Process control statement

Special statements (using, lock, etc)

Three structures of the program: Sequence Structure, selection structure, and cyclic structure.

Sequential structure: it is executed from top to bottom in order of writing.

Select structure: perform different subsequent steps based on different conclusions.

If statement: if

Standard If statement format: if () {} else {}

If statement else if form: if () {} else {}

Switch (expression)

{

Case constant expression 1: ------ attention symbol

Statement 1.

Break;

Case constant expression 2:

Statement 2.

Break;

Default:

Statement to be executed 3.

Break;

}

. (The value after case must be a constant expression. Variables and values of any two cases cannot be the same .)

Loop Structure: the so-called loop refers to repeated execution of some statements

Four loop statements in C:

While (condition) // This condition is also a Boolean expression

{

Loop body statement;

}

First judgment, recycling

Do

{

Loop body statement;

} While (test condition); execute the loop statement first and then judge

Difference between do-while and while: do-while executes the statements in the loop first, and then judges whether the condition is true. If it is true, the loop continues. If it is false, the loop ends. Therefore, the do-while statement must be executed at least once. The while statement first checks whether the condition is true. If the condition is true, the loop statement is executed. If the condition is not true, the loop is terminated. Therefore, the while statement may not execute the loop body statement once.

For:

For (initial values of cyclic variables; conditions for cyclic termination; value-added values of cyclic variables) {cyclic body statement}

Foreach

One-dimensional array initialization:

Two-dimensional array initialization:

Class and object:

Basic concepts of object-oriented programming:

The main idea of Object-Oriented Programming (OOP) is to encapsulate data and operations on such data into a data structure called class. When using this class, you only need to define a class variable, which is called an Object ). Use the class by calling the members of the object.

In object-oriented theory, class is the abstraction (or induction) of a class of things with the same features ).

Understanding of the relationship between classes and objects:

Class: a set of objects with the same data structure and operation. To use an object, you must first define the class and then create an object.

Class creation:

A class is composed of method members and data members (variables and constants.

The responsibility of the method is to implement the behavior of the class, the status of the field storage class,

What technologies are available for object programming: attributes, methods, constructors, inheritance, interfaces, overloading, polymorphism, delegation, and events.

Class member access modifier:

1. public: indicates that any class can access this member without restriction.

2. private: indicates that the class or method is only visible to the methods in this class.

3. protected: This class can be accessed and inherited from its subclass.

4. internal: the class or method is visible inside the Assembly. It is invisible to the external class of the set of programs, that is, in the same.. NET collection or all types in the database can be accessed evenly, and can be accessed in the same namespace.

5. protected internal: The same. NET set or subclass that inherits this class can be accessed.

Role: Controls external access to class members to hide data.

The static member belongs to the class, and the instance member belongs to the class object instead of the class.

Classes are composed of data members and function members. They are the attributes and methods of classes.

Field and local variable:

Field: a variable declared as an object or value type at the class level. It can be understood as a common global variable.

Local variables: variables declared in methods, events, and constructors.

Compared with simple data types, classes are complex abstract data types defined by users. Compared with simple variables, objects are complex data units that include both data and method code.

Face three main features of object programming:

Encapsulation-hide information that the caller does not need to know.

Inheritance-simplify the design of classes.

Polymorphism-provides different implementation methods for methods with the same name.

Only get is a read-only attribute, and only set is a write-only attribute.

Function Definition:

Class Name

{

// Static method

[Method modifier] static return value type method name (parameter list) // method Header

{

...... // Method body

}

// Non-static (instance method)

[Method modifier] Return Value Type method name (parameter list) // method Header

{

...... // Method body

}

}

Object lifecycle:

Each object has a clearly defined life cycle, which has three stages:

1. Construction phase: completed by the constructor, the object is initially instantiated

2. Normal use

3. destructor phase: C # is completed by the destructor. After the object is used up, some cleanup operations are performed, such as releasing the memory.

What is constructor:

Constructor is a special method. The object used to initialize the class (including the data stored in the object ). The constructor must be the same as the class name. The constructor does not return values and cannot use the void keyword. The constructor is called by the compiler. Your code cannot explicitly call the constructor.

C # language: Put the initialization work of the object in the constructor, and put the cleanup work in the destructor. When an object is created, the constructor is automatically executed. When an object dies, the Destructor is automatically executed.

Implement inheritance-define a derived class:

[Access modifier] class derived class name: Base class Name

{

// Program code

}

C # only supports single-class inheritance: A class can only have one parent class (base class)

String processing:

Char represents a Unicode Character in C #, which constitutes a string.

Escape characters:

\ N newline \ t jump horizontally to the next tabulation position, skip the grid

\ V vertical jump grid \ B backspace

\ R press enter \ f form feed

\ Backslash \ 'single quotes

Difference between String and string:

String represents a Unicode character String.

String is the alias of System. String in. NET Framework.

Essence: String is the class name and string is the alias

String processing:

(1) string connection

Either use the "concat" method or use the "+" operator.

(2) case-insensitive Conversion

Convert string to uppercase: ToUpper ()

Convert string to lowercase: ToLower ()

(3) search strings

Whether the string ends with the specified string: EndsWith ()

Is there any specified string in the string: IndexOf ()

If it is found, the index number of the string is returned.

If not found,-1 is returned.

(4) compare strings

Equals ()

Compare ()

CompareTo ()

(5) copy a string

Copy ()

CopyTo ()

(6) truncation string Substring ()

You can also use indexes to retrieve a character from a string.

Console. WriteLine (str1 [3]);

// Extract the fourth character from the string.

(7) Format the string: Format ()

Prototype: public static string Format (string format, object obj)

(8) separator string: Split ()

Prototype: public string [] Split (params char [] separator)

(9) insert and fill strings

Insert: Insert ()

Fill: PadLetf ()/PadRight ()

(10) delete a string: Remove ()

(11) replacement string: Replace ()

Prototype:

(12) Delete the Left and Right spaces of the string: Trim ()

(13) String Length: Length attribute

Variable string -- StringBuilder

The StringBuilder class has six different constructor methods.

The StringBuilder class exists in the System. Text namespace and is commonly used to operate strings.

Append (), Insert (), Remove (), Replace ()

Difference between the StringBuilder class and the String class:

The String object is immutable and will be re-created to allocate space when it needs to be modified, resulting in high system overhead.

StringBuilder is different. For example, the StringBuilder class can improve performance by modifying strings repeatedly instead of creating new objects.

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.