• C#和C++中char类型的区别


    对于char,这个字符类型。我们一般都认为就是一个字节。今天在仔细比较发现,C#的char和C++的char是有区别的。

    1.首先来看C#中char占多大空间

    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine(sizeof(char));
                Console.Read();
            }
        }
    }

    image

    居然是返回2.也就是说两个字节。

    2. 在C++中呢?

    #include <iostream>
    using namespace std;
    int main()
    {
        cout << sizeof(char)<<endl;
        return 0;
    }

    image

    这里看到的结果是显示为1个字节

    但是同时,我又想起来,C++里面还有另外一个char类型,也就是所谓的wchar_t,通常用来表示unicode char,它的空间是多少呢?

    #include <iostream>
    using namespace std;
    int main()
    {
        cout << sizeof(wchar_t)<<endl;
        return 0;
    }

    image

    3. 那么,是不是说C#中的char都是表示unicode字符的呢?

    没错,就是这样.  如此才能解释得通嘛

    http://msdn.microsoft.com/zh-cn/library/x9h8tsay.aspx

    char 关键字用于声明下表所示范围内的 Unicode 字符。Unicode 字符是 16 位字符,用于表示世界上大多数已知的书面语言。

    类型

    范围

    大小

    .NET Framework 类型

    char

    U+0000 到 U+ffff

    16 位 Unicode 字符

    System.Char

    4. 题外话:SQL Server 中的字符类型

    我还想到,在SQL Server的类型系统中有下面几个字符类型,大家也要有所比较

    定长 char
    定长(unicode) nchar
    变长 varchar
    变长(unicode) nvarchar

    也就是说,在SQL Server中也是明确地区分unicode和非unicode的

  • 相关阅读:
    js动态生成按钮,页面用DIV简单布局
    Maven初学之经验浅谈
    pl/sql注册码
    windows server 2012R2 网络慢的那些事
    sql 优化
    巧用selectKey
    list集合,map集合遍历
    oracle中declare程序块用法
    处理oracle锁表
    关于img标签图片不加载不识别相对路径得解决办法
  • 原文地址:https://www.cnblogs.com/chenxizhang/p/1498270.html
Copyright © 2020-2023  润新知