您的位置:首页 > 编程语言 > C#

C# Best Practices - Handling Strings

2016-01-26 11:54 483 查看
Features

Strings Are Immutable.

A String Is a Reference Type

Value Type

Store their data directly

Example: int, decimal, bool, enum

Reference Type

Store references to their data

Example: string, object

String Method Best Practices

Do:

Look at what .NET provides before writing your own

Use Intellisense to view the list of available methods

Avoid:

Calling string methods on null strings

Handling Nulls

String.IsNullOrWhiteSpace(cendor)

vendor?.ToLower();

Do:

Write unit tests that cover null conditions

Use IsNullOrWhiteSpace when testing for null in a block of code

Use the null-conditional operator ? when checking for null in a single statement

Verbatim String Literal

var directions = @"Insert \r\n to define a new line";

Insert \r\n to define a new line

Do:

Use verbatim string literals when the string contains special characters such as backslashes.

Use verbatim string literals to hold folder or file names, @"c:\mydir\myfile.txt"

Use two quotes to include quotes in a verbatim string literal, @"Say it with a long ""a"" sound"

Avoid:

Using verbatim string literals when there is no reason, @"Frodo"

String.Format

Do:

Use String.Format to insert the value of an expression into a string

Include a formatting string as needed, like String.Format("Deliver by:{0:d}", deliveryBy))

Avoid:

Use String.Format when concatenating string literals, like String.Format("Hello {0}", "World");

String Interpolation

var pc = String.Format("{0}-{1}", product.Category, product.SequenceNumber);

var pc = $"{product.Category}-{product.SequenceNumber}";

StringBuilder

Conceptually a mutable string

Allow string operations, such as concatenation, without creating a new string

Provides methods for manipulating the mutable string - Append, Insert, Replace, etc

Use ToString to convert a string

More efficient when working with strings that are

- Build up with many separate concatenation operations

- Changed a large number of times, such as within a loop

Do:

Use StringBuilder when building up a string with numerous concatenation operations

Use StringBuilder when modifying a string numerous times (such as a loop)

Consider readability

Avoid:

Use StringBuilder when only modify a string a few times

FAQ

1.What does it mean to say that C# strings are immutable?

It means that strings cannot be modified once they are created.

2.Is a string a value type or a reference type?

A string is a reference type, but acts like a value type.

3.What is the best way to check for null string?

Use String.IsNullOrWhiteSpace is great when checking nulls for a code block

Use the new C# 6 null-conditional operator ? is great for code statements

4.What are the benefits to using StringBuilder?

The .NET StringBuilder class is mutable, meaning that it can be readily changed.

Using StringBuilder is therefore more efficient when appending lots of strings.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: