c# 中几个关于string问题

1、string是一个应用类型,而不是值类型;为什么用起来很像值类型?因为微软对其做了特殊处理。

2、

 1 using System;
 2 
 3 namespace testForString
 4 {
 5     class Program
 6     {
 7         class Abc
 8         {
 9             public string A { get; set; }
10 
11             public DateTime D { get; set; }
12         }
13 
14         static void Main(string[] args)
15         {
16             Abc abc = new Abc();
17 
18 
19             string str = null + "acb";
20 
21             Console.ReadKey();
22         }
23     }
24 }

以上代码中abc变量中abc.A为null,abc.D不是空而是1年1月1日0点0分0秒000。

  

 string str = null + "acb";

 

代码并没有抛出异常。而是str被赋值为“abc”

3、如果是这样的一句话,

 string str; 

string abc=str+null; 

运行时,会编译不通过.

4、运行以下代码:

1 string str=null;
2 
3 string abc=str+null;
4 
5 Console.WriteLine(abc.toString());

不会抛出任何一个异常,在第二行中abc已经被赋值为“”。

5.运行以下一段代码: 

 1 using System;
 2 
 3 namespace testForString
 4 {
 5     class Program
 6     {
 7         static void Main(string[] args)
 8         {
 9             string str = null;        
10 
11             Console.WriteLine(str.ToString());
12 
13             Console.ReadKey();
14         }
15     }
16 }

 将会抛出错误:System.NullReferenceException。

能完全答对的举手!!!

 

java中会是什么样子呢?

 

1         String str=null;
2         String str1=str+null;
3          
4         System.out.println(str1+"abc");
5         
6         System.out.println(str1.toString());
7         System.out.println(str.toString());

 

第4行打印出:nullnullabc

第6行打印出:nullnull

第7行抛出空指针错误:

Exception in thread "main" java.lang.NullPointerException
at cn.test.timer.TestTimer.main(TestTimer.java:xx)

 

posted @ 2016-06-07 00:41  cctext  阅读(468)  评论(0编辑  收藏  举报