码迷,mamicode.com
首页 > 其他好文 > 详细

类库探源——System.ValueType

时间:2014-12-11 01:32:49      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   ar   color   os   sp   for   strong   

一、MSDN描述

ValueType 类:提供值类型的基类

命名空间: System

程序集:   mscorlib.dll

继承关系:

bubuko.com,布布扣

 

值类型包括:字符、整数、浮点、布尔、枚举、结构(其实字符、整数、浮点、布尔是结构,下面会说明)

 

二、值类型花名册

1. 字符

Char 结构: 表示一个 Unicode 字符。

命名空间:   System

程序集   : mscorlib.dll

原型定义:

[SerializableAttribute]
[ComVisibleAttribute(true)]
public struct Char : IComparable, IConvertible, IComparable<char>, IEquatable<char>

在C# 中 char 是 System.Char 的别名

System.Char 继承 System.ValueType

using System;

class App
{
    static void Main()
    {
        var charType1 = typeof(Char);
        var charType2 = typeof(char);    // int 是 Int32 在C#中的别名
        
        Console.WriteLine(charType1);
        Console.WriteLine(charType1.BaseType);        
        Console.WriteLine(charType2);
    }
}

bubuko.com,布布扣

常用属性和方法:

IsDigit(Char)           是否是数字

IsLetter(Char)          是否是字母

IsLetterOrDigit(Char)   是否是数字或字母

 

2. 整数

分为有符号整数和无符号整数

有符号整数

SByte <--> sbyte      8位

Int16 <--> short       16位

Int32 <--> int          32位

Int64 <--> long        64位

无符号

Byte  <--> byte

UInt16 <--> ushort

UInt32 <--> uint

UInt64 <--> ulong

 

3. 浮点

单精度

Single  <--> float

双精度

Double <--> double

 

4. 布尔

Boolean  <--> bool

 

5. 枚举

Enum 类:为枚举提供基类

命名空间:   System

程序集 :   mscorlib.dll

原型定义:

[SerializableAttribute]
[ComVisibleAttribute(true)]
public abstract class Enum : ValueType, IComparable, IFormattable, IConvertible

可以看出 Enum 是class

在C# 中 System.Enum 的别名为 enum

 

常见属性和方法:

Parse(Type, String)        解析枚举值

TryParse<TEnum>(String, TEnum) 

 

6. 一些常见的结构 struct

1. Char、Int16、Int32、Int64、Single 、Double 以及无符号版本

 

2. IntPtr 结构:用于表示指针或句柄的平台特定类型

常用构造器:

IntPtr(Int32)

IntPtr(Int64)

var intPtr1 = new  IntPtr(23222);
var intPtrZero = IntPtr.Zero; // 代表已初始化为零的指针或句柄

 

3. Guid 结构: 表示全局唯一标识符 (GUID)

常用构造器:

Guid(String)

 

常用属性和方法:

Guid.Empty        Guid 类的只读实例,其值保证均为零

Guid.NewGuid()  产生一个新Guid

Guid.Parse

Guid.TryParse

ToString

 

4. DateTime结构 : 表示时间上的一刻,通常以日期和当天的时间表示

 

5. TimeSpan结构:表示一个时间间隔

 

6. Nullable<T> 结构:表示基础类型为值类型的对象,值类型与引用类型一样也可以分配 null

 

未完

类库探源——System.ValueType

标签:style   blog   http   ar   color   os   sp   for   strong   

原文地址:http://www.cnblogs.com/Aphasia/p/4156683.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!