C#零碎 一

C#零碎

  1. 单引号引起来代表字符,双引号引起来代表字符串
  2. Main方法中传入的参数 static int Main(string[] args){}, args参数是用于接收命令行参数的字符串数组
  3. c#6.0之后的字符串插值功能,在字符串字面值前加$前缀
class HeyYou
 {
       static void Main()
     {
             string firstname="ezio";
             string lastname="wu";
              System.Console.WriteLine($"Your full name is{firstname}{lastname}")
     }
 }
  1. bool的实际大小是一个字节
  2. C#允许在字符串前使用@符号,指明转移序列不被处理,以@开头的字符串唯一支持的转义序列是“”,代表一个双引号,不会终止字符串
  3. 字符串插值内部工作原理
    字符串插值是调用string.Format()方法的语法糖。例如以下语句
System.Console.WriteLine($"Your full name is{firstname}{lastname}")

会被转换成以下形式的c#代码:

object[] args=new object[] {firstname,lastname};
Console.WriteLine(string.Format("Your full name is {0}{1}",args));
  1. null值表明变量不引用任何有效的对象,void表示无类型,或者没有任何值,只能将null赋值给引用类型,指针类型和可空值类型,void本质上不是数据类型,它只是指出没有数据类型这一事实
  2. 有可能造成数据丢失或引发异常(因为转换失败)的任何转换都需要执行显式转换 ,相反,不会丢失数据,而且不会引发异常(无论操作数的类型是什么)的任何转换都可以进行隐式转换
  3. 一般不能将null值赋给值类型,这是因为根据定义,值类型不能包含引用,即使对“什么都没有”的引用。为了声明能存储null的变量,要使用可空修饰符"?"
static void Main()
{
     int? count = null;
      do
      {

      }
      while (count == null);
            
}
  1. 数组中Length返回数组的元素个数,GetLength()返回指定维数的长度,Rank()返回维数

  2. foreach循环的特点是每一项只遍历一次,不会像其他循环那样出现计数错误,也不可能越过集合边界

  3. 返回引用有两个重要的限制,两者都和对象生存期有关:对象引用在任被引用时不应该被垃圾收回,而且不存在任何引用时不应消耗内存。为符合这些限制,从方法返回引用时只能返回:
    (1)对字段或数组元素的引用
    (2)其他返回引用的属性或方法
    (3)作为参数传给“返回引用的方法”的引用

  4. 方法重载是一种操作多态性

  5. 异常处理中,catch块必须从最具体到最不具体排列,例如

class HelloWorld
    {
        static int Main(string[] args)
        {
            string firstName;
            string ageText;
            int age;
            int result = 0;

            WriteLine("Hey you");

            Write("Enter your first name:");
            firstName = ReadLine();

            Write("Enter your age:");
            ageText = ReadLine();

            try
            {
                age = int.Parse(ageText);
                WriteLine($"Hi {firstName}! You are {age * 12} months old");
            }
            catch (FormatException)
            {
                WriteLine($"The age entered,{ageText},is not valid");
                result = 1;
            }
            catch (Exception exception)
            {
                WriteLine($"Unexpected error:{exception.Message}");
                result = 1;
            }
            finally
            {
                WriteLine($"Goodbye{firstName}");
            }
            return result;
        }
    }

System.Exception数据类型最不具体,所以它应该放到最后。

  1. Parse()方法的问题在于,要知道转换能否成功,唯一的办法就是尝试执行类型转换,并在失败时抛出并捕捉异常。由于异常处理代价搞,所以更好的办法是尝试执行转换,同时不进行异常处理。方法TryParse()就提供了这种操作,它返回bool值而不是转换好的值,要求使用out关键字,也就是和ref对应的输出参数“out”,例如
static void Main(string[] args)
        {
            string firstName;
            string ageText;

            WriteLine("Hey you");

            Write("Enter your first name:");
            firstName = ReadLine();

            Write("Enter your age:");
            ageText = ReadLine();

            if (int.TryParse(ageText, out int age))
            {
                WriteLine($"{firstName}" + $"You are {age * 12} months old");
            }
            else
            {
                WriteLine($"The age entered,{ageText},is not vaild");
            }
        }

或者

static void Main(string[] args)
        {
            string firstName;
            string ageText;
			int age;
            WriteLine("Hey you");

            Write("Enter your first name:");
            firstName = ReadLine();

            Write("Enter your age:");
            ageText = ReadLine();

            if (int.TryParse(ageText, out age))
            {
                WriteLine($"{firstName}" + $"You are {age * 12} months old");
            }
            else
            {
                WriteLine($"The age entered,{ageText},is not vaild");
            }
        }
  1. 静态方法不能直接访问类的实例字段
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值