kainster

never too late
posts - 33, comments - 3, trackbacks - 0, articles - 0
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

《CLR via C#》 Chap11 字符、字符串和文本

Posted on 2008-12-26 11:11 kainster 阅读(198) 评论(0)  编辑  收藏 所属分类: CLR via C#
using System;
using System.Collections.Generic;
using System.Text;

namespace chap11
{
    
class Program
    
{
        
static void Main(string[] args)
        
{
            
char c;
            
int i = 70000;
            
try
            
{
                c 
= (char)i;
                Console.WriteLine(c);
            }

            
catch(Exception e)
            
{
                Console.WriteLine(e.Message);
            }

            
try
            
{
                c 
= Convert.ToChar(i);
                Console.WriteLine(c);
            }

            
catch(Exception e)
            
{
                Console.WriteLine(e.Message);
            }

            Console.Read();
        }

    }

}




不要在字符串中硬编码 \n \r等,
string s = "Hi\r\nthere";不太好
而应该用 string s = "Hi" + Environment.NewLine + "there";

string是不可变的,相关的操作返回的都是一个新的string。如果要在运行时将几个字符串连接到一起,请避免使用+操作符,因为它会在垃圾收集堆上创建多个字符串对象,相反,应当使用System.Text.StringBuilder类型。

以下写法完全等价
string file = "C:\\Windows\\System32\\NotePad.exe";
string file = @"C:\Windows\System32\NotePad.exe";


只有注册用户登录后才能发表评论。


网站导航: