C # data type (2)

String

char a collection of string name = "John Doe"; Double quotes, char is a single quote
string is immutable, once it is initialized, it cannot be changed. Every time you use the existing stringAfter the operation, a new one is actually createdstring, and then assign the new value to this variable, the old value It was cleaned up by the framework.
Share pictures

thisNew assignment and clean up old The process of value has been repeated 10,000 times, which is not smart enough. It is recommended to use StringBuilder
 Share picture

Append can be used if StringBuilder is used Method to expand string, this efficiency should be higher than that


string operator
+ Connect two or more strings (you can add other types to ToString first and then +)
Format is the same format as python, regardless of the original data type, {} will do.
Share a picture

Length  Get string length
SubString  Get String
IndexOf  Find the first index of a certain character
Replace  Replace
Contains  Whether it contains a certain character or string
Startswith  What starts with
Endswith   ends with

< p style="margin: 0px 0px 1.2em !important;"> The meaning of \ escape character is not mentioned, the universe is universal
@   If you think \ is used too much and it is not intuitive, you can add it in front of string as a whole @ Symbol, so what is in the string is nothing, it will not be escaped, see the picture below
Share a picture

Data & Time

Share pictures

The default is: date+time
When formatting, m means minutes and M means month

< p style="margin: 0px 0px 1.2em !important;">The time display caused by cultural differences in different regions can be set by setting System.Globalization.CultureInfo to solve
share picture
Parse   and TryParse
The latter will not report an error when parsing, and it is more secure


empty type null

Normal data types cannot be assigned to null的,  int a = null ;This is the compiler will report an error
The method is to add a question mark after the data type ,?   So this amount can be assigned to null (It can be assigned to null, instead of having to give him a value of null)
Share a picture
After assigning the value to null, of course you can assign it a new value Value
nullable = 123;

Share pictures

and the above if< /code>Compared with two judgments, there is a value and it is 42, and the output It's 42!
The following is GetValueOrDefault< /code> method, return the value if there is a value, it is null will return the default value of the basic type (int’s default value is 0) It’s more concise than the above to use


var keyword

We can put a var to implicitly define its data type
share picture

The above int is an explicit definition< br>name is an implicit definition, not explicitly stated as string The compiler sees what you give name is assigned string< /code>, he puts name is defined as string
This is as fast as the display definition, don’t worry about the efficiency difference

var can only be used in method Can not be used in class level

Sometimes we use var, or directly use the return value of the function as the variable type (I don’t care what type is returned, I var result = someMethod() will do)
share picture

share picture

Above, one var Let us reduce the number of keyboard letters, you have to say that I can copy and paste the previous line and modify it. . . .

In most cases, we can put var to a fixed type, but in anonymous type (anonymous types) must be used, I'll talk about this later.

?

String

char collectionstring name = "John Doe"; Double quotes, char is a single quote
string is immutable, once initialized It can’t be changed, every time the existing string After the operation, a new string, and then assign the new value to this variable, the old value will be cleaned up by the framework
Share Picture

thisThe process of creating new assignments and cleaning up old values has been repeated 10,000 times, which is not smart enough. It is recommended to use StringBuilder
Share pictures

Using StringBuilder, you can use Append method to extend string, this efficiency should be higher than that


string operator
+   connect two or more strings (you can put other types ToString first and then +)
Format The format is the same as python, regardless of the original data type, {} will do.
Share a picture

Length  Get string length
SubString  Get string
IndexOf   Find the first index of a character< br>Replace  Replace
Contains   Does it contain a certain character or string
Startswith   What starts with
Endswith   Ends with?

\ I won’t talk about the meaning of escape characters.
@   If you think \ is used too much and it is not intuitive, you can add the @ symbol in front of the string as a whole, so that what is in the string is what it is and will not be escaped , Look at the picture below
share picture

Data & Time

share picture

The default is: Date+Time
When formatting, m means minutes and M means month

different Time display caused by regional cultural differences can be set by setting System.Globalization.CultureInfo to solve
Share pictures
Parse   and TryParse
The latter will not report errors when parsing, and it is more secure


Null type null

Normal data types cannot be assigned to null,   int a = null ; This is the compiler will report an error
The method is to add a question mark after the data type, ?   So this amount can be assigned to null (It can be assigned to null, instead of having to give him a value of null)
Share a picture
After assigning the value to null, of course you can assign it a new value Value
nullable = 123;

Share pictures

and the above if< /code>Compared with two judgments, there is a value and it is 42, and the output It's 42!
The following is GetValueOrDefault method, the return value is null will return the default value of the basic type (int的默认值是0)比上面的更加简洁使用


var关键字

我们可以在一个变量左边放一个var 来含蓄的定义它的数据类型
分享图片

上面的 int 是显式的定义
下面的name是隐式的定义,没有明确说是string 编译器看你给name 赋值的是string,他就把name定义为string
这和显示定义 是一样快的,不用担心有啥效率上的差别

var 只能用在method 里,不能用在class级别里

有时候我们为了简化代码就用了var,或者直接用函数的返回值作为变量的类型(我不用管返回什么类型,我var result = someMethod()就行了)
分享图片

分享图片

上图,一个var 让我们少打 了多少键盘字母,您非要说 把上一行复制粘贴修改 我也没办法。 . . .

大多数情况下,我们是可以把var 换成某种固定的类型的,但是在匿名类型(anonymous types)里必须得用,这个以后再讲。

?

?

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3051 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.