首页 > 代码库 > C# 基础

C# 基础

1.枚举

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6  7 namespace Test 8 { 9     public enum TimeOfDay10     {11         Morning = 0,12         Afternoon = 1,13         Evening = 214     }15     class Program16     {17         static void Main(string[] args)18         {19             TimeOfDay time = TimeOfDay.Afternoon;20             Console.WriteLine(time.ToString()); //输出:Afternoon21 22             TimeOfDay time2 = (TimeOfDay)Enum.Parse(typeof(TimeOfDay), "afternoon", true);23             Console.WriteLine((int)time2);24         }25     }26 }27 //Enum.Parse(枚举类型,需转换的字符串,bool(是否忽略大小写)),返回对象(此处TimeOfDay.Afternoon),需显示转换。

 

2.数组:必须同类型

1 int[] arrays = new int[32];2 Console.WriteLine(arrays.Length);//返回数组长度3 4 int[] arrays2;5 Console.WriteLine(arrays2.Length);//报错

 

3.命名空间-别名

1 using System;2 using Introduction = Wrox.ProCSharp.Basics;3 4 //...5 6 Introduction::NamespaceExample NSEx = new Introduction::NamespaceExample(); //命名空间别名修饰符::

 

4.Console.Write()

1 int i = 730;2 int j = 73;3 Console.WriteLine("{0,4}\n+{1,4}\n----\n{2,4}", i, j, i + j);4 decimal m = 940.23m;5 decimal n = 73.7m;6 Console.WriteLine("{0,9:C2}\n+{1,9:C2}\n------\n{2,9:C2}", m, n, m + n);7 //{x,y:z} x表示参数索引,y表示宽度值(正值右对齐,负值左对齐),z表示格式字符串,如C2表示精度到小数点后两位或者{x,y:#.00}

 

5.C#变成规则

Pascal大小写形式:命名空间,类以及基类中成员等。如EmployeeSalary;

camel大小写形式:

1 //类中所有私有成员字段的名称2 public int subscriberId;3 4 //成员字段通常下划线开始5 public int _subscriberId;6 7 //传递的形参数8 public void RecordSale(string salemanName, int quantity);

 

C# 基础