C # data types (2)

String

charSet of  string name = "John Doe";double quotes, charsingle quotes
stringare immutable and can not be changed after once initialized, each of the existing stringpost-operation, in fact created a new string, and then assigns the new value of this variable, the old the value is to clean up the framework

This 新建 赋值 清理旧值process is repeated 10,000 times, not smart enough, recommendedStringBuilder

Use the StringBuilder Append method can be used to extend the string, this efficiency should be higher than that of some


string operator
+ connecting two or more of the string (and then can put another type ToString +)
the Format python with the same format form, and leave the original data type, line {}

Length Gets the string length
SubString get string
IndexOf find the first index of a character
Replace to replace
Contains contains a character or string
StartsWith start with what
Endswith to what end

\ Escape character meaning is not to say that the universe GM
@ If you think \ is not intuitive use too much, you can string the whole front of the @ symbol, which is what this string is valid and will not be escaped, see The following figure

Data & Time

Default: date + time
formatting when m represents minute, M represents the month

Time cultural differences lead to different areas of the display can be set up System.Globalization.CultureInfoto solve

Parse and TryParse
the latter not being given time to resolve, more Insurance


Null null type

Normal data type can not be assigned to null,  int a = null ;so that the compiler will complain of
is to add a question mark after the data type,   so that this amount can be assigned null(can be assigned to null, and not have to give him an assignment to null)

assigned to the null value in the new course can be assigned to him
nullable = 123;

And the above ifcomparison, two were determined, and has a value of 42, the output It‘s 42!
following the GetValueOrDefaultmethod, has the value returns the value is nullreturned to its default value of the base type ( intthe default value is 0) is more compact than the above use


var keyword

We can put one on the left of a variable varto implicitly define its data type

The above intis an explicit definition of
the following nameis an implicit definition does not explicitly say that stringthe compiler to look at your nameassignment is string, he took namedefined as stringthe
this display and is defined as fast, do not worry about what's on efficiency difference

varCan only be used in methodyears, it can not be used at classlevels in

Sometimes we have to simplify the code on the use of var, or directly with the return value of the function as the type of the variable (I do not control what type of return, I var result = someMethod()will do)

The figure, a varlet's play a little keyboard letter how much you have to say to copy and paste one line modification I have no idea. . . .

In most cases, we can put varinto some kind of fixed type, but in an anonymous type ( anonymous typeshave to use), the revisit this later.

Guess you like

Origin www.cnblogs.com/springbrotherhpu/p/11564070.html