2017-9-19c# notes (LINQ standard Operator, String class, String method, struct, static constructor, enumeration type, bit ID)

Source: Internet
Author: User
Tags modifiers

    1. Standard query operators in LINQ

There are two kinds of syntax when writing LINQ: Query syntax and method syntax, where the method syntax is in the form of a command, and it uses a standard method call. A method is a set of methods called standard query operators.

The standard query operator has a series of methods called APIs that allow us to query any. NET data collection. Important features for standard query operators include the following:

(1) The binding object being queried is called a sequence, it must implement the Ienumerable<t> interface, T is the type;

(2) Standard query operators use method syntax

(3) Some operators return ienumberable objects (or other sequences) two other operators return scalars. The return scalar operator executes immediately, and the second returns the value of the object of the other section enum type is deferred iteration.

Standard operators provide query functionality including filtering, projection, aggregation, sorting, and more.

2.String class

The string class represents text, which is a series of Unicode characters. In the namespace system, strings are an ordered collection of Unicode codes that represent text. A string object is an ordered collection of System.Char objects that represent strings. The value of the string object is the contents of the ordered collection, and the value is immutable.

An index is the position of a Char object in a string, not the location of a Unicode character. The index is a zero-based, non-negative number that is counted from the location of the string (its index is 0). Successive index values may not correspond to contiguous Unicode characters.

To use each Unicode character instead of each Char object, you use the System.Globalization.StringInfo class. A member of the string class disables the comparison of a string object, the index of a character or string within a string object, the value of a string object, the splitting of a character or combination of strings, the modification of a string's value, or a number, date, time, or enumeration that is worth formatting to a string ; normalize a string.

There are two properties for the string class:

(1) Chars: Gets the character at the specified character position in the current string object

(2) Length: Gets the number of characters in the current string object

    1. String method

(1) Compare to two characters to achieve the comparison function, the return is the result of two string comparison of the value, that is, the relationship of data,

(2) Concat connection

(3) Contains returns a Boolean type that indicates whether the specified string object appears in this string

(4) Endwith returns a Boolean type that determines whether the end of the string matches the specified string

(5) Stertwith returns a Boolean type that determines whether the start of a string matches the specified string

(6) Whether Equals is equal

(7) format replaces one or more personal format items in the specified string with the string representation of the specified object

(8) IndexOf Report specifies the index of the first occurrence of a character or string in this example, similar to the Lastofany,lastindexof,lastindexany method.

(9) Insert method inserts a specified string instance as specified in this example.

Remove Delete

(one) Replace

Split implements the ability to split multiple substrings by a specified character or string

SubString implements the ability to take substrings of a string at a specified starting position and a specified length

ToUpper and ToLower case-sensitive conversions

Trim method to delete all leading and trailing white space characters in a string

    1. StringBuilder class

The StringBuilder class represents a variable character string because it can be modified after it is created by appending, removing, replacing, or inserting a Word II.

The capacity of the StringBuilder is the maximum number of characters that an instance can store at any given time, and is greater than or equal to the length of the string representation of the instance value. Capacity can be increased or decreased by either the capacity property or the Ensurecapacity method, but it cannot be less than the value of the length property. If no capacity or maximum capacity is specified when initializing an instance of StringBuilder, the implementation-specific default value is used.

The string object concatenation operation always creates new objects with existing strings and data.

The StringBuilder object maintains a buffer to accommodate the concatenation of new data. If there is not enough space, the new data is appended to the end of the buffer, otherwise a new, larger buffer is allocated, the data in the original buffer is copied to the new buffer, and the new data is appended to the new buffer. The performance of a concatenation operation on a string or StringBuilder object depends on how often memory allocations occur. The string concatenation operation flies with memory each time, whereas the stringbuild concatenation operation allocates memory only if the StringBuilder object buffer is too small to accommodate the new data.

If a fixed number of string objects are concatenated, the string class is more suitable for concatenation operations, and if any string is concatenated, the StringBuilder object is more appropriate for the operation.

StringBuilder-Tired properties:

(1) Chars: Gets or sets the character at the specified character position in the current StringBuilder object

(2) Length: Gets or sets the length of the current StringBuilder object.

(3) Capacity: Gets or sets the maximum number of characters that can be included in the memory allocated by the current instance

(4) Maxcapacity: Gets the maximum capacity for this instance.

Methods in the StringBuilder class:

(1) Append method: Implements appends the specified data type to the end of this instance to a string representation.

(2) Clear method: Removes a character from the current StringBuilder instance.

(3) Equals method: Returns a value that indicates whether the instance is equal to the specified object

(4) Insert

(5) Replace

(6) ToString Method

    1. Structural body
      A struct is a data type of a value type that can be defined within a class based on its use range selection, or defined outside of all tired. In general, struct types that are defined inside a class are used only in that class, and structures that are defined outside the class can be used within the scope of the current namespace. Structs in C # are not defined inside of a containing or method.

The following additions to the structure are provided:

(1) A struct is a keyword that defines a struct type, and does not represent a data type.

The struct keyword cannot be used as the data type of a variable until a specific struct type is defined using the STRCT keyword.

(2) struct type definition a new database type, not a variable, is declared to the compiler with the help of the STUCT keyword. There is no new storage space allocated for this data type, so it is not possible to apply the operation of variables such as assigning values directly to data types defined using the struct keyword, or to initialize members of struct types.

(3) A member of a struct type can be either a simple data type or a struct type, that is, the definition of a struct allows nesting.

(4) Recursive definitions of struct types are not supported in C #, that is, members of struct types are not allowed to be types of this struct.

(5) struct type use a pair of braces to define the role of a sub-type of the province, U so its member name can be the same as the external identifier. struct types can use semicolons after the right curly brace}.

(6) struct in C # In addition to the data members, it also has function members. The biggest difference between a struct and a class is that a class is a reference type, a two-struct is a value type, a struct is sealed, cannot be inherited, and an allocation structure consumes less than an instance of the allocated class, so it is especially useful for small data structures with value type semantics.

6. Static constructors

Struct types can also have static constructors, and static constructors are used to initialize static data members. Static constructors have the following characteristics "

(1) Static constructors cannot have access modifiers and parameters

(2) Static constructors cannot access instance members

(3) Static constructors cannot be called directly

The trigger rules for static functions of structs and classes are different, and the tired static constructors are called automatically before the first instance of the piece is referenced or referenced by any static member, and the static function of the two structures is called in the following cases:

Initialized with the constructor that displays the declaration;

Invoking a struct's method or accessing the structure's static data members (whether read or assigned, accessing instance data members does not trigger the CLR to automatically invoke the constructor of the lectern).

When assigning a value to a struct object, it is essentially assigning the entire member of an object's memory space to another object's storage space. When a struct type includes a large number of outgoing data members, the assignment of a struct object can be time consuming.

7. Enumeration type

Each enum type has an underlying integer type, which defaults to int. Each member is assigned a constant value of the underlying type; By default, the first member of the compiler is assigned a value of 0, and the value of each subsequent member is 1 more than the previous member;

If the value of the enumeration constant is established, its subsequent enumeration constants are incremented by 1 from the current enumeration constant.

You need to be aware of using enum types:

(1): Like the structure chart type, enumerations are value types, so they store their data directly, rather than storing data and references separately.

(2) enumeration of members with only one type: named Integer value constants;

(3) You cannot use modifiers on members of an enumeration type. They both implicitly have and enumerate the same accessibility;

(4) because the members of an enumeration type are constants, they can be accessed even when there are no variables of that enumeration type. Access to enumeration type members can be implemented using enumeration type names, member reference operators, and member names

(5) Members of different enumeration types are not allowed to compare, even if their structure and name are identical;

(6) In general, it is better to define the enumeration directly in the namespace so that all classes in that namespace can access it as easily as possible. However, enumerations can also be nested within a class or struct;

(7) The underlying type specifies the storage size allocated for each enumeration, but conversions from an enum type to an integral type need to be done with a display type conversion.

8. Bit ID

Programmers use different bits of the word for a long time as a compact way to represent a set of on/off flags. Enumeration types provide an easy way to achieve this:

(1) Determine how many flag bits are required and select a sufficient number of unsigned types to hold it.

(2) Determine the meaning of each bit representation, and name it, declare an enumeration of the selected integer type, each member has a bit position representation.

(3) Use a bit or operator to set the appropriate bit in the word holding the bit flag;

(4) Use bits and operators to unlock bit flags.

For example, the following enumeration

[Flages]

Enum Carddecksetting:unit

{

singledeck=0x01; bit 0

largepicture=0x02; bit 1

Fancynumber = 0x04;w bit 2

animation=0x08; bit 3

}

Implementing tag enumeration requires attention:

(1) Add [flages] tag at the top of the enumeration declaration;

(2) The values of each enumerated item should conform to the law of power of the exponent of 2.

2017-9-19c# notes (LINQ standard Operator, String class, String method, struct, static constructor, enumeration type, bit ID)

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.