C# - test and cast with reference type or value...
2013-05-30 00:00
429 查看
Please check the following code snippet for test and cast on references types (exemplified by a string object), or a value type , represented by a int type.
also, the special case of 0 is considered.
also, the special case of 0 is considered.
[Test] public void TestConvertNullToString() { var a = ((string) null); if (a == null) { Console.WriteLine("Hello"); } else { Console.WriteLine("bbb"); } if (a is string) { Console.WriteLine("a is String"); } else { Console.WriteLine("a is not String"); } if (a is int) { Console.WriteLine("a is int"); } else { Console.WriteLine("a is not int"); } // if ((a as int) == null) // { // Console.WriteLine("a is not String");} // // } // else // { // Console.WriteLine("a is not String"); // } int c = 1; object d = c; a = ((string) d); // Invalid cast exception Assert.DoesNotThrow(() => { string.IsNullOrEmpty((string) null); }); } } }
相关文章推荐
- C#: reference type and value type
- C#值类型与引用类型(Value Type and Reference Type)
- C# pass reference type by value
- Unity c# reference value type
- Effective C#之6:Distinguish Between Value Type and Reference Type
- Value Type And Reference Type
- CLR Via C# 3rd 阅读摘要 -- Chapter 5 - Primitive, Reference, and Value Types
- Set up your first C# test with NUnit or resharper
- Entity references or sequences beginning with an ampersand ‘&’ must be terminated with a semicolon ‘;’. 符号&必须要分号结束
- Working principles about stack, heap, garbage collector, value type and reference type
- Test-Driven C# Improve the Design and Flexibility of Your Project with Extreme Programming Techniques
- Primitive, Reference, and Value Types(Chapter 5 of CLR via C#)
- .NET框架-string是value or reference type?
- Effective C# Item 6: Distinguish Between Value Types and Reference Types
- Err:The "." operator was supplied with an index value of type "java.lang.String" to be applied to a List or array
- C# 2012 step by step 学习笔记8 CHAPTER 9 Creating Value types with enumerations and Structures
- not-null property references a null or transient value : org.com.model.person_one_one_test.address
- reference and value type
- C#中值类型和引用类型(value type&Reference tyoe)深度探讨
- Test-Driven C# Improve the Design and Flexibility of Your Project with Extreme Programming Techniques