C#知识点(为自己整理)

C#中char[]与string之间的转换
string 转换成 Char[]
string ss = "abcdefg";
char[] cc = ss.ToCharArray();
Char[] 转换成string
string s = new string(cc);
此外,byte[] 与 string 之间的装换
byte[] bb = Encoding.UTF8.GetBytes(ss);
string s = Encoding.UTF8.GetString(bb);
我们也可以利用 StringBuilder 来进行数组 与 string 间的转换
using System.Text;
StringBuilder sb = new StringBuilder();
foreach(char c in cc)
    sb.Append(c);
string s = sb.ToString();

///Object.ReferenceEquals(Object, Object) Method

如果 objA 是与 objB 相同的实例,或两者均为 null,则为 true;否则为 false

object o = null; object p = null; object q = new Object(); Console.WriteLine(Object.ReferenceEquals(o, p)); p = q; Console.WriteLine(Object.ReferenceEquals(p, q)); Console.WriteLine(Object.ReferenceEquals(o, p));

/* This code produces the following output. True True False */

注解

与不同Equals方法和相等运算符ReferenceEquals不能重写方法。 因此,如果你想要测试两个对象是否相等的引用而您不确定的实现Equals方法时,可以调用ReferenceEquals方法。

但是,返回值的ReferenceEquals方法似乎存在异常中这两种方案:

  • 比较值类型。 如果objAobjB是值类型,它们装箱传递到之前ReferenceEquals方法。 这意味着,如果这两个objAobjB表示的值类型的相同实例ReferenceEquals方法不过返回false

  • int int1 = 3; Console.WriteLine(Object.ReferenceEquals(int1, int1)); Console.WriteLine(int1.GetType().IsValueType);

  • // The example displays the following output: // False // True

  • 当比较字符串。 如果objAobjB都是字符串,ReferenceEquals方法将返回true如果字符串暂留。 它不会执行测试的值相等。 在以下示例中,s1s2因为它们是单一的暂存字符串的两个实例是否相等。 但是,s3s4是否不相等,因为尽管它们都具有相同的字符串值,但该字符串未被暂存。

  • String s1 = "String1"; String s2 = "String1"; Console.WriteLine("s1 = s2: {0}", Object.ReferenceEquals(s1, s2)); Console.WriteLine("{0} interned: {1}", s1, String.IsNullOrEmpty(String.IsInterned(s1)) ? "No" : "Yes"); String suffix = "A"; String s3 = "String" + suffix; String s4 = "String" + suffix; Console.WriteLine("s3 = s4: {0}", Object.ReferenceEquals(s3, s4)); Console.WriteLine("{0} interned: {1}", s3, String.IsNullOrEmpty(String.IsInterned(s3)) ? "No" : "Yes");

  • // The example displays the following output: // s1 = s2: True // String1 interned: Yes // s3 = s4: False // StringA interned: No

 

c# 类型 信息/

            //byte类型从0 -> 255。
            byte xb = 1; //整型1隐式转换成byte类型.如果整数大于255,则产生异常(错误)
            Console.WriteLine(xb.GetType());
            // output System.Byte 
            Console.WriteLine(sizeof(System.Byte)); // 1

            //doesn't work
            //byte xb1 = 256;

            short xs = 2;
            Console.WriteLine(xs.GetType());
            //output System.Int16
            Console.WriteLine(sizeof(System.Int16));// 2

            int xi = 1;
            Console.WriteLine(xi.GetType());
            //output System.Int32
            Console.WriteLine(sizeof(System.Int32));// 4
            
            long xl = 3;
            Console.WriteLine(xl.GetType());
            //output System.Int64
            Console.WriteLine(sizeof(System.Int64));// 8

            //doesn't work , ushort,uint,ulong表示无符号的short,int,long,普通点说,就是没有负数,只有正数和0
            //ushort xus = -1;
            ushort xus = 1;
            Console.WriteLine(xus.GetType());
            //output System.UInt16
            Console.WriteLine(sizeof(System.UInt16));// 2

            uint xui = 2;
            Console.WriteLine(xui.GetType());
            //output System.UInt32
            Console.WriteLine(sizeof(System.UInt32));// 4

            ulong xul = 3;
            Console.WriteLine(xul.GetType());
            //output System.UInt64
            Console.WriteLine(sizeof(System.UInt64));// 8

            decimal xd = 2; //整型2隐式转换成decimal类型
            Console.WriteLine(xd.GetType());
            //output System.Decimal
            Console.WriteLine(sizeof(System.Decimal)); // 16
            Console.WriteLine(xd);// 2

            float xf = 2f;
            Console.WriteLine(xf.GetType());
            //output System.Single -- 单精度
            Console.WriteLine(sizeof(System.Single));// 4

            double xdo = 2; // 整型2隐式转换成double类型
            Console.WriteLine(xdo.GetType());
            //output System.Double -- 双精度
            Console.WriteLine(sizeof(System.Double));// 8
            Console.WriteLine(xdo);// 2

            Console.ReadLine();

/

 

nt32 lTemp   =   Convert.ToInt32("0xa10b", 16); 
这样的话,就将0xa10b这个十六制形式的字符串转为十进制的数字了十进制的数字了

C++ int型进行高16位和低16位的操作


                    int l16 =int 类型 >> 16;
                    int h16 = int 类型  & 0x0000ffff;

int a = 1254;

        int a1 = (a & 0x00FF0000);
        int a11 = a1 >> 16;

        int a2 = (a & 0x0000FF00);
        int a22 = a2 >> 8;

        int a3 = a & 0x000000FF;

        var a6 = Convert.ToString(a, 2);


        int g = 42;

        int g1 = g >> 1;
        int g11 = g1 & 0x1;

        int g2 = g >> 2;
        int g21 = g2 & 0x1;

        int g3 = g >> 3;
        int g31 = g3 & 0x1;

        int g4 = g >> 4;
        int g41 = g4 & 0x1;

        int g5 = g >> 5;
        int g51 = g5 & 0x1;

        int g6 = g >> 6;
        int g61 = g6 & 0x1;

 

 

//字符串转ASCII码
string str = "abcd";
byte[] bytetest = System.Text.Encoding.Default.GetBytes(str);


//字符转ASCII码:
public static int Asc(string character)
{
  if (character.Length == 1)
  {
  System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
  int intAsciiCode = (int)asciiEncoding.GetBytes(character)[0];
  return intAsciiCode;
  }
  else
{
throw new Exception("Character is not valid.");
}
}


ASCII转字符串
string str = char.ConvertFromUtf32(65)


//ASCII码转字符:
public static string Chr(int asciiCode)
{
  if (asciiCode >= 0&& asciiCode <= 255)
  {
  System.Text.ASCIEncoding asciiEncoding = new System.TextASCIlEncoding();
  byte[]byteArray = new byte[]{(byte)asciiCode };
  string strCharacter = asciiEncoding.GetString(byteArray);
  return strCharacter;
  }
  else
  {
  throw new Exception("ASCII Code is not valid.");
  }
}

 

用C#怎么判定一个数是整数还是小数?

我的方法是把一个变量定义成float,如下:
float a;
if (a - (int)a != 0);   ///这样可以确定是小数;
方法2:
float a;
if (a % 1 != 0);   ///这样也行;

可是这些方法都不能判定诸如1.0   2.0  之类的小数

解决:float a;

......//运算对a赋值

string str_a=Convert.ToString(a);
int dot=str_a.IndexOf(".");
string substr=a.Substring(dot+1);
bool hasnotzerochar=false;//记录是否小数点后存在不为0的字符
for (i=0;i<substr.Length();i++)
{
if (substr[i]!="0")
{
hasnotzerochar=true;
}
}
这样,你只需要判断hasnotzerochar是否为true就可以知道了

 

浮点数相等的判断(由于浮点数有误差, 所以判断的时候最好不要用等号,尤其是计算出来的结果)
if (Mathf.Approximately(1.0, 10.0/10.0))  

print ("same");  

 

 

C#中判断字符串相等的方法

 

可以使用如下方式:

1. String.Compare(str1, str2) == 0  或者  str1.CompareTo(str2) == 0

2. str1.Equals(str2)  或者 String.Equals(str1, str2)
这种方式的话,需要注意null的情况

3. str1 == str2
这是因为String.Equals方法已经重载了,和==是一样的效果。

 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
东南亚位于我国倡导推进的“一带一路”海陆交汇地带,作为当今全球发展最为迅速的地区之一,近年来区域内生产总值实现了显著且稳定的增长。根据东盟主要经济体公布的最新数据,印度尼西亚2023年国内生产总值(GDP)增长5.05%;越南2023年经济增长5.05%;马来西亚2023年经济增速为3.7%;泰国2023年经济增长1.9%;新加坡2023年经济增长1.1%;柬埔寨2023年经济增速预计为5.6%。 东盟国家在“一带一路”沿线国家中的总体GDP经济规模、贸易总额与国外直接投资均为最大,因此有着举足轻重的地位和作用。当前,东盟与中国已互相成为双方最大的交易伙伴。中国-东盟贸易总额已从2013年的443亿元增长至 2023年合计超逾6.4万亿元,占中国外贸总值的15.4%。在过去20余年中,东盟国家不断在全球多变的格局里面临挑战并寻求机遇。2023东盟国家主要经济体受到国内消费、国外投资、货币政策、旅游业复苏、和大宗商品出口价企稳等方面的提振,经济显现出稳步增长态势和强韧性的潜能。 本调研报告旨在深度挖掘东南亚市场的增长潜力与发展机会,分析东南亚市场竞争态势、销售模式、客户偏好、整体市场营商环境,为国内企业出海开展业务提供客观参考意见。 本文核心内容: 市场空间:全球行业市场空间、东南亚市场发展空间。 竞争态势:全球份额,东南亚市场企业份额。 销售模式:东南亚市场销售模式、本地代理商 客户情况:东南亚本地客户及偏好分析 营商环境:东南亚营商环境分析 本文纳入的企业包括国外及印尼本土企业,以及相关上下游企业等,部分名单 QYResearch是全球知名的大型咨询公司,行业涵盖各高科技行业产业链细分市场,横跨如半导体产业链(半导体设备及零部件、半导体材料、集成电路、制造、封测、分立器件、传感器、光电器件)、光伏产业链(设备、硅料/硅片、电池片、组件、辅料支架、逆变器、电站终端)、新能源汽车产业链(动力电池及材料、电驱电控、汽车半导体/电子、整车、充电桩)、通信产业链(通信系统设备、终端设备、电子元器件、射频前端、光模块、4G/5G/6G、宽带、IoT、数字经济、AI)、先进材料产业链(金属材料、高分子材料、陶瓷材料、纳米材料等)、机械制造产业链(数控机床、工程机械、电气机械、3C自动化、工业机器人、激光、工控、无人机)、食品药品、医疗器械、农业等。邮箱:market@qyresearch.com

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值