C# 编码规范

这份文档提供了编写健壮且可靠的C#程序的指导原则,包括文件组织、缩进、注释、声明、命名约定和编程实践等。建议每个类放在单独的文件中,文件名与类名一致,并按命名空间创建目录结构。
摘要由CSDN通过智能技术生成

先猜个谜再看吧

两只蚂蚁一起慢慢的爬着,忽然一只蚂蚁发现一个大
大的梨子。(猜两个国家的名)
谜底在文章未

 

Technotes, HowTo Series

C# Coding Style Guide

Version 0.3
by Mike Krüger icsharpcode.net

  1. About the C# Coding Style Guide
  2. File Organization
  3. Indentation
  4. Comments
  5. Declarations
  6. Statements
  7. White Space
  8. Naming Conventions
  9. Programming Practices
  10. Code Examples

1. About the C# Coding Style Guide

This document may be read as a guide to writing robust and reliable programs. It focuses on programs written in C#, but many of the rules and principles are useful even if you write in another programming language.

2. File Organization

2.1 C# Sourcefiles

Keep your classes/files short, don't exceed 2000 LOC, divide your code up, make structures clearer. Put every class in a separate file and name the file like the class name (with .cs as extension of course). This convention makes things much easier.

2.2 Directory Layout

Create a directory for every namespace. (For MyProject.TestSuite.TestTier use MyProject/TestSuite/TestTier as the path, do not use the namespace name with dots.) This makes it easier to map namespaces to the directory layout.

3. Indentation

3.1 Wrapping Lines

When an expression will not fit on a single line, break it up according to these general principles:

  • Break after a comma.
  • Break after an operator.
  • Prefer higher-level breaks to lower-level breaks.
  • Align the new line with the beginning of the expression at the same level on the previous line

    Example of breaking up method calls:

    longMethodCall(expr1, expr2,
                   expr3, expr4, expr5);

    Examples of breaking an arithmetic expression:

    PREFER:

    var = a * b / (c - g + f) +
          4 * z;

    BAD STYLE – AVOID:

    var = a * b / (c - g +
          f) + 4 * z;

    The first is preferred, since the break occurs outside the paranthesized expression (higher level rule). Note that you indent with tabs to the indentation level and then with spaces to the breaking position in our example this would be:

     > var = a * b / (c - g + f) + > ......4 * z;  

    Where '>' are tab chars and '.' are spaces. (the spaces after the tab char are the indent with of the tab). A good coding practice is to make the tab and space chars visible in the editor which is used.

    3.2 White Spaces

    An indentation standard using spaces never was achieved. Some people like 2 spaces, some prefer 4 and others die for 8, or even more spaces. Better use tabs. Tab characters have some advantages:

  • Everyone can set their own preferred indentation level
  • It is only 1 character and not 2, 4, 8 … therefore it will reduce typing (even with smartindenting you have to set the indentation manually sometimes, or take it back or whatever)
  • If you want to increase the indentation (or decrease), mark one block and increase the indent level with Tab with Shift-Tab you decrease the indentation. This is true for almost any texteditor.

    Here, we define the Tab as the standard indentation character.

    Don't use spaces for indentation - use tabs!

    4. Comments

    4.1 Block Comments

    Block comments should usually be avoided. For descriptions use of the /// comment

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值