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. 工作日志2014-06-10(实现C语言解析XML获得查询关键字)

    #include "GetInfo.h" ]; int GetInfoToWrite(char* path, char* domain,Write_t* pwrite); int ...

  2. 为什么要for循环以及for循环的流程

    /* Name:为什么需要循环以及for循环流程 Copyright: By.不懂网络 Author: Yangbin Date:2014年2月10日 03:16:55 Description:求1 ...

  3. delphi 7 下安装 indy 10.5.8 教程

    本教程用 indy 10.5.8 替换 delphi 7 自带的 indy 版本,让大家深入了解 delphi 组件安装的方法. 第一步:下载 indy 10.5.8 组件,解压到合适的目录里.如 D ...

  4. 编译器DIY——词法分析

    在上一篇文章中已经介绍了读文件的操作,那么这一篇文章中将会细致解释词法分析. 在源文件里解析出的单词流必须识别为保留字,标识符,常量,操作符和界符五大类 1.显然我们须要列举出全部的保留字,而这里与保 ...

  5. Android设置Activity背景为透明style

    方法一: 通过Theme.Translucent @android:style/Theme.Translucent @android:style/Theme.Translucent.NoTitleBa ...

  6. uploadify 使用 详细 说明

    简单 常用的 时间 有点 紧  先写 这点 ,  有时间 在更新 下面有 例子  链接 不过要 自己 摘 一下 很简单的啦   <script type="text/javascrip ...

  7. GBK转utf-8,宽字符转窄字符

    //GBK转UTF8 string CAppString::GBKToUTF8(const string & strGBK) { string strOutUTF8 = "" ...

  8. Linux内核学习笔记-1.简介和入门

    原创文章,转载请注明:Linux内核学习笔记-1.简介和入门 By Lucio.Yang 部分内容来自:Linux Kernel Development(Third Edition),Robert L ...

  9. codevs 1515 跳 贪心+lucas

    题目链接 一个人初始在(0, 0), 想到(n, m)去, 没到一个格子, 花费的值为C(n, m), 求最小值. C(n, m)的定义为, 如果n==0||m==0, 则为1, 否则C(n, m) ...

  10. ssh无密登录

    ssh登录一般两种方式: 1.密码登录 2.密钥验证无需密码 使用方式:1.生成密钥 2.将公钥追加到authorized_keys中,需要注意的是执行权限需为600,这里因而第一次添加使用的是> ...