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.

C# Best Practices - Handling Strings的更多相关文章

  1. [翻译] GONMarkupParser

    GONMarkupParser https://github.com/nicolasgoutaland/GONMarkupParser NSString *inputText = @"Sim ...

  2. str_replace vs preg_replace

    转自:http://benchmarks.ro/2011/02/str_replace-vs-preg_replace/ 事实证明str_replace确实比preg_replace快. If you ...

  3. MySQL 中的 FOUND_ROWS() 与 ROW_COUNT() 函数

    移植sql server 的存储过程到mysql中,遇到了sql server中的: IF @@ROWCOUNT < 1 对应到mysql中可以使用 FOUND_ROWS() 函数来替换. 1. ...

  4. nodejs(三)Buffer module & Byte Order

    一.目录 ➤ Understanding why you need buffers in Node ➤ Creating a buffer from a string ➤ Converting a b ...

  5. Zend API:深入 PHP 内核

    Introduction Those who know don't talk. Those who talk don't know. Sometimes, PHP "as is" ...

  6. Java实现LeetCode_0020_ValidParentheses

    package javaLeetCode.primary; import java.util.Scanner; import java.util.Stack; public class ValidPa ...

  7. OSCP Learning Notes - Privilege Escalation

    Privilege Escalation Download the Basic-pentesting vitualmation from the following website: https:// ...

  8. Java – Top 5 Exception Handling Coding Practices to Avoid

    This article represents top 5 coding practices related with Java exception handling that you may wan ...

  9. Exception (3) Java exception handling best practices

    List Never swallow the exception in catch block Declare the specific checked exceptions that your me ...

随机推荐

  1. 解决Flex4 发布后访问 初始化极其缓慢的问题

    原文http://blog.163.com/vituk93@126/blog/static/170958034201282222046364/ 昨天找了个免费.net空间,想测试一下做的一个简单Fle ...

  2. Java泛型的一点用法(转)

    1.一个优秀的泛型,建议不要这样写public static <K, V> Map<K, V> getMap(String source, String firstSplit, ...

  3. //Build/ 2014 开发者大会Azure重点整理

     寓教于乐,轻松掌握 Windows Apps和 Cloud //Build/ 2014开发者大会第二天重点整理 (上) //Build/ 2014开发者大会第二天的主题演讲主要包含两部分:Mic ...

  4. Extjs4 类的定义和扩展

    一般定义方式,注意方法和函数的添加方式不同.(添加函数只能用override方式添加不知为什么,有知道的,请搞之.) 定义一个类,并给他一个方法 1: Ext.define('Simple.Class ...

  5. linux 使用ssh到远端并且使用while的坑

    如果要使用ssh批量登录到其它系统上操作时,我们会采用循环的方式去处理,那么这里存在一个巨大坑,你必须要小心了. 现在是想用一个脚本获取远程服务器端/root下面的文件: #!/bin/bash ca ...

  6. hough变换是如何检测出直线和圆的?

    (I)直线篇 1 直线是如何表示的?对于平面中的一条直线,在笛卡尔坐标系中,常见的有点斜式,两点式两种表示方法.然而在hough变换中,考虑的是另外一种表示方式:使用(r,theta)来表示一条直线. ...

  7. VS的工程宏,比如$(SolutionDir) 的含义及查找

    Configuration->General->Output Directory->单击编辑框点下拉箭头-> <Edit...> 图1 图2

  8. BZOJ 2751: [HAOI2012]容易题(easy)( )

    有限制的最多就K个, 所以我们处理一下这K个就行了. 其他可以任选, 贡献都是∑i (1≤i≤N), 用快速幂. ------------------------------------------- ...

  9. 【转】C#实现MD5加密

    转自:C#实现MD5加密 常用的如下: 方法一 首先,先简单介绍一下MD5 MD5的全称是message-digest algorithm 5(信息-摘要算法,在90年代初由mit laborator ...

  10. CDOJ 1259 昊昊爱运动 II bitset+线段树

    题目链接 昊昊喜欢运动 他N天内会参加M种运动(每种运动用一个[1,m]的整数表示) 现在有Q个操作,操作描述如下 昊昊把第l天到第r天的运动全部换成了x(x∈[1,m]) 问昊昊第l天到第r天参加了 ...