惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
有赞技术团队
有赞技术团队
WordPress大学
WordPress大学
博客园 - 司徒正美
D
Docker
B
Blog
V
Visual Studio Blog
Blog — PlanetScale
Blog — PlanetScale
U
Unit 42
S
SegmentFault 最新的问题
小众软件
小众软件
J
Java Code Geeks
美团技术团队
腾讯CDC
MyScale Blog
MyScale Blog
爱范儿
爱范儿
H
Help Net Security
宝玉的分享
宝玉的分享
Microsoft Azure Blog
Microsoft Azure Blog
A
About on SuperTechFans
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
博客园 - 【当耐特】

酷 壳 – CoolShell

感染新冠的经历 | 酷 壳 - CoolShell 从一次经历谈 TIME_WAIT 的那些事 | 酷 壳 - CoolShell 网络数字身份认证术 | 酷 壳 - CoolShell 我做系统架构的一些原则 | 酷 壳 - CoolShell 源代码特洛伊木马攻击 | 酷 壳 - CoolShell Go编程模式 : 泛型编程 | 酷 壳 - CoolShell 如何做一个有质量的技术分享 | 酷 壳 - CoolShell Go 编程模式:k8s Visitor 模式 | 酷 壳 - CoolShell Go编程模式:Pipeline | 酷 壳 - CoolShell Go编程模式:委托和反转控制 | 酷 壳 - CoolShell Go 编程模式:Go Generation | 酷 壳 - CoolShell Go编程模式:Map-Reduce | 酷 壳 - CoolShell Go 编程模式:错误处理 | 酷 壳 - CoolShell Go编程模式:切片,接口,时间和性能 | 酷 壳 - CoolShell 百度为什么掉队了 | 酷 壳 - CoolShell 程序员如何把控自己的职业 | 酷 壳 - CoolShell 计时攻击 Timing Attacks | 酷 壳 - CoolShell Rust语言的编程范式 | 酷 壳 - CoolShell HTTP的前世今生 | 酷 壳 - CoolShell 记一次Kubernetes/Docker网络排障 | 酷 壳 - CoolShell 可视化编程 | 酷 壳 - CoolShell 程序的本质复杂性和元语言抽象 | 酷 壳 - CoolShell 伙伴分配器的一个极简实现 | 酷 壳 - CoolShell C++11的Lambda使用一例:华容道求解 | 酷 壳 - CoolShell C++面试中string类的一种正确写法 | 酷 壳 - CoolShell C++模板”>>”编译问题与词法消歧设计 | 酷 壳 - CoolShell 数据即代码:元驱动编程 | 酷 壳 - CoolShell 数据的游戏:冰与火 | 酷 壳 - CoolShell 7个示例科普CPU Cache | 酷 壳 - CoolShell 加班与效率 | 酷 壳 - CoolShell
初学C#编程的注意事项 | 酷 壳 - CoolShell
陈皓 · 2009-08-31 · via 酷 壳 – CoolShell

下面是8个C#编程时的注意事项是给初学者的,可能你知道,也可能你不知道,不过这些都是一些可能会让人疏忽的地方,还是要注意一下。

1.使用String变量:

考虑有下面的一个程序想判断一下字符串是否有内容。

if (someString.Length > 0)
{
    // …
}

但是,这个字符串对象很可能是个空对象,所以,最好先判断一下null

if  (!String.IsNullOrEmpty(someString))
{
    // 是不是更好一些?
}

2.字符器连接

string s = “dev”;
s += “-”;
s += “the”;
s += “-”;
s += “web”;
s += “.”;
s += “com”;

这样做没什么问题,只不过性能不高,因为+=操作符其实调用的是String类的Append访问,所以,+=会有两次函数调用,下面的则会好一些。

StringBuilder s = new StringBuilder();
s.Append(”dev”);
s.Append(”-”);
s.Append(”the”);
s.Append(”-”);
s.Append(”web”);
s.Append(”.”);
s.Append(”com”);

3.使用Console

Console.WriteLine("A= " + 1 + " B=" + 2 + " C= " + someValue);

和第二点说的一样,这并没有效率,使用下面的语句,会更有效率。

Console.WriteLine(”A: {0}\nB: {1}\nC: {2}”, 1, 2, someValue);

4.字符串转整型

int i = int.Parse(Request.QueryString["id"]);

这样做的问题是,如果有人这样请求你的页面:yourpage.aspx?id=A6,那么A6将会导致你的程序抛出一个异常。因为A6不是一个整数字符串。使用TryParse会好一点。

int i;
if (!int.TryParse(Request.QueryString["id"] , out i))
{
    //…
}

5. 调用IDbConnection 的 Close 方法

IDbConnection dbConn = null;

try
{
    dbConn = new SqlConnection(”some Connection String”);
    dbConn.Open();
}
finally
{
    dbConn.Close();
}

调用SqlConnection的构造函数可能会出现一个异常,如果是这样的话,我们还需要调用Close方法吗?

IDbConnection dbConn = null;

try
{
    dbConn = new SqlConnection(”Some Connection String”);
    dbConn.Open();
}
finally
{
    if (dbConn != null)
    {
        dbConn.Close();
    }
}

6.使用List类

public void SomeMethod(List<SomeItem> items)
{
    foreach(var item in items)
    {
        // do something with the item…
    }
}

如果我们只是遍历List容器中的所有内容的话,那么,使用IEnumerable接口会更好一些。因为函数参数传递一个List对象要比一个IEnumerable接口要花费更多的开销。

public void SomeMethod(IEnumerable<SomeItem> items)
{
    foreach(var item in items)
    {
        // do something with the item…
    }
}

7.直接使用数字

if(mode == 1) { … }
else if(mode == 2) { … }
else if(mode == 3) { … }

为什么不给你的这些数字取个名字呢?比如使用Enumerations。

public enum SomeEnumerator
{
    DefaultMode = 1,
    SafeMode = 2,
    NormalMode = 3
}

if(mode == SomeEnumerator.DefaultMode) { … }
else if(mode == SomeEnumerator.SafeMode) { … }
else if(mode == SomeEnumerator.NormalMode) { … }

8.字符串替换

string s = "www.coolshell.cn is a amazing site";
s.Replace("amazing", "awful");

字符串s的内容什么也不会改变,因为string返回的是替换过的字串。这点很多初学者经常忘了。下面就没有问题了。

s = s.Replace("amazing", "awful");

文章:来源

Loading...