C#编码规范

释放双眼,带上耳机,听听看~!

记录 编码约定 学习过程。

 

命名空间约定

如果没有使用using指令,项目也没有默认导入合适的命名空间,访问这些命名空间或者类型时,则需要“完全限定名称”。


1
2
3
4
5
6
7
8
9
10
11
12
1namespace ConsoleApp4
2{
3    class Program
4    {
5        static void Main(string[] args)
6        {
7            // 在这里System.Diagnostics是“完全限定名称”
8            var traceSource = new System.Diagnostics.TraceSource("");
9        }
10    }
11}
12

如果使用了Using指令,则不需要“完全限定名称”。


1
2
3
4
5
6
7
8
9
10
11
12
13
1using System.Diagnostics;
2
3namespace ConsoleApp4
4{
5    class Program
6    {
7        static void Main(string[] args)
8        {            
9            var traceSource = new TraceSource("");
10        }
11    }  
12}
13

 

代码布局约定

  1. 不轻易更改编辑器的设置,通常使用默认的,特别是格式设置和制表符。

C#编码规范

  1. 每行一条语句,每行一个声明。


1
2
3
4
5
6
7
8
1string[] strs = new string[] { "AA","BB","CC"};
2string str = "hello";
3int num = 4;
4if (num == 9)
5{
6
7}
8
  1. 一行写不完的语句,断行写则需要缩进一个制表符位,除了起头的第一行,后面无论再断多少行都是一个制表符位。


1
2
3
4
1string[] strs = new string[] { "AA","BB","CC"};            
2var query = strs.Where(x => x.Length == 2 && x.Contains("B")).
3    Select(x => new { Name = x, Age = 8 });
4
  1. 属性和方法之间至少一个空行


1
2
3
4
5
6
7
8
1public int MyProperty { get; set; }
2public int MyProperty1 { get; set; }
3
4void Test()
5{
6
7}
8
  1. 使用括号突出表达式的字句。


1
2
3
4
5
6
7
8
9
1int val1 = 1;
2int val2 = 3;
3int val3 = 4;
4
5if ((val1 > val2) && (val1 > val3))
6{
7    
8}
9

注释约定

单独的行,而非代码末尾;大写字母开头,断行换小写;句点结束注释文本;注释和文本之间留一个空格。

不要在注释周围创建格式化的星号块???没看懂。

下面放一段ILSpy源码


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1/// <summary>
2/// Sets the value of a dependency property on <paramref name="targetObject"/> using a markup extension.
3/// </summary>
4/// <remarks>This method does not support markup extensions like x:Static that depend on
5/// having a XAML file as context.</remarks>
6public static void SetValueToExtension(this DependencyObject targetObject, DependencyProperty property, MarkupExtension markupExtension)
7{
8    // This method was copied from ICSharpCode.Core.Presentation (with permission to switch license to X11)
9    
10    if (targetObject == null)
11        throw new ArgumentNullException(nameof(targetObject));
12    if (property == null)
13        throw new ArgumentNullException(nameof(property));
14    if (markupExtension == null)
15        throw new ArgumentNullException(nameof(markupExtension));
16    
17    var serviceProvider = new SetValueToExtensionServiceProvider(targetObject, property);
18    targetObject.SetValue(property, markupExtension.ProvideValue(serviceProvider));
19}
20

 

隐式类型var

当明显可以从赋值右侧推导左侧变量类型时,使用var。当精确的类型不太重要时,使用var。


1
2
3
4
1var var1 = "This is clearly a string.";
2var var2 = 27;
3var var3 = Convert.ToInt32(Console.ReadLine());
4

右侧类型不明确时,则可以声明一个明确的类型。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
1using System;
2
3namespace ConsoleApp4
4{
5    class Program
6    {
7        static void Main(string[] args)
8        {
9            string test = GetObject(2);
10            int test1 = GetObject(1);
11            decimal test2 = GetObject(3);
12        }
13
14        static dynamic GetObject(int x)
15        {
16            switch (x)
17            {
18                case 1:return 2;
19                case 2:return "";
20                default:return 8m;
21            }
22        }
23    }  
24}
25

在for循环使用var,下面的i分别推导为int、decimal。


1
2
3
4
5
6
7
8
1for (var i = 0; i < 10000; i++)
2{              
3}
4
5for(var i = 0m; i < 10m; i = i + 0.1m)
6{
7}
8

在foreach中不建议使用var。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1DataTable dataTable = new DataTable();
2// 枚举值:EnumerableRowCollection<DataRow>,可以推row为DataRow。
3foreach (var row in dataTable.AsEnumerable())
4{
5
6}
7// 虽然可以推,但声明明确的类型会让代码更加清晰,因为无法在枚举泛型类型里明显看出当前row的类型。
8foreach (DataRow row in dataTable.AsEnumerable())
9{
10
11}
12// 枚举值:DataRowCollection,不声明明确类型row则为object?,因此需要声明明确的类型。
13foreach (DataRow row in dataTable.Rows)
14{
15
16}
17

符号类型

使用时,符号类型优先于无符号类型。short、int、long。 ushort、uint、ulong。

数组

使用间接的语法声明数组


1
2
3
4
5
6
7
8
9
10
1// 这时候,无法从右侧推导变量类型。
2string[] vowels1 = { "a", "e", "i", "o", "u" };
3
4// 可以推导,左侧使用var
5var vowels2 = new string[] { "a", "e", "i", "o", "u" };
6
7var vowels3 = new string[2];
8vowels3[0] = "a";
9vowels3[1] = "e";
10

委托

使用简短的语法为委托赋值。


1
2
3
4
5
6
7
8
9
10
11
12
13
1static void Main(string[] args)
2{            
3    Del exampleDel2 = DelMethod;
4    var del3 = new Del(DelMethod);
5}
6
7public delegate void Del(string message);
8
9public static void DelMethod(string str)
10{
11    Console.WriteLine("DelMethod argument: {0}", str);
12}
13

Using

如果使用try…finally…,且在finally只有释放资源的代码,考虑替换成using,using会自动调用Dispose方法。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1Font font1 = new Font("Arial", 10.0f);
2try
3{
4
5}
6finally
7{
8    font1.Dispose();
9}
10
11using(Font font=new Font("Arial", 10.0f))
12{
13
14}
15

New运算符

能推导类型使用var,对象实例的构造函数没有参数则不必调用构造函数。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1// 不必使用new ExampleClass()
2var instance3 = new ExampleClass
3{
4    Name = "Desktop",
5    ID = 37414,
6    Location = "Redmond",
7    Age = 2.3
8};
9
10var instance4 = new ExampleClass();
11instance4.Name = "Desktop";
12instance4.ID = 37414;
13instance4.Location = "Redmond";
14instance4.Age = 2.3;
15

事件处理

在事件比较简短、且只使用一次的情况下,可以考虑使用lambda表达式。


1
2
3
4
5
6
7
8
9
10
1public Form1()
2{
3    InitializeComponent();
4    
5    this.Click += (s, e) =>
6    {
7        MessageBox.Show(s.ToString());
8    };
9}
10

** 静态成员**

 派生对象时,不建议添加和基类具有相同名称的静态成员。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
1using System;
2
3namespace ConsoleApp4
4{
5    class Program
6    {
7        static void Main(string[] args)
8        {
9            ExampleClass2.Test();
10        }      
11    }
12
13    class ExampleClass
14    {
15        public static void Test()
16        {
17            Console.WriteLine("调用ExampleClass Test");
18        }
19    }
20    class ExampleClass2:ExampleClass
21    {
22        // 同名容易引起混乱,隐性覆盖了ExampleClass的Test方法。
23        public static void Test()
24        {
25            Console.WriteLine("调用ExampleClass2 Test");
26        }        
27    }
28
29    class ExampleClass3 : ExampleClass
30    {
31        // 使用new,说明有意覆盖。
32        public new static void Test()
33        {
34
35        }
36    }
37}
38

** Linq查询表达式**

 匿名类型遵循Pascal大小写格式(
与骆驼命名法类似,骆驼命名法是首字母小写,而Pascal帕斯卡命名法是首字母大写)。


1
2
3
4
5
6
7
8
9
1var localDistributors = from customer in customers
2                        join distributor in distributors on customer.City equals distributor.City
3                        select new
4                        {
5                            Customer = customer,
6                            Distributor = distributor,
7                            CustometorId = customer.ID
8                        };
9

在查询变量和范围变量的声明中使用隐式类型化,虽然查询变量没有明确标有var关键字,但确实隐式化了。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
1using System;
2using System.Linq;
3
4namespace ConsoleApp4
5{
6    class Program
7    {
8        static void Main(string[] args)
9        {
10            var customers = new System.Collections.Generic.List<Customer>();
11            var distributors = new System.Collections.Generic.List<Distributor>();
12
13            var localDistributors = from customer in customers
14                                    join distributor in distributors on customer.City equals distributor.City
15                                    select new
16                                    {
17                                        Customer = customer,
18                                        Distributor = distributor,
19                                        CustometorId = customer.ID
20                                    };
21
22        }
23    }
24
25    internal class Distributor
26    {
27        public object City { get; internal set; }
28    }
29    internal class Customer
30    {
31        public object City { get; internal set; }
32        public object ID { get; internal set; }
33    }
34}
35

对其from字句下面的查询语句。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
1// 第一种:from语句不和声明变量同一行,则在下一行缩进一个制表符。  
2var localDistributors =
3    from customer in customers
4    join distributor in distributors on customer.City equals distributor.City
5    select new
6    {
7        Customer = customer,
8        Distributor = distributor,
9        CustometorId = customer.ID
10    };    
11          
12// 第二种:from语句和声明变量同一行
13var localDistributors = from customer in customers
14                        join distributor in distributors on customer.City equals distributor.City
15                        select new
16                        {
17                            Customer = customer,
18                            Distributor = distributor,
19                            CustometorId = customer.ID
20                        };
21

 

给TA打赏
共{{data.count}}人
人已打赏
安全技术

详解Node.js API系列 Crypto加密模块

2021-12-21 16:36:11

安全技术

从零搭建自己的SpringBoot后台框架(二十三)

2022-1-12 12:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索