C#文件编写约定

本文关键字:约定 文件 | 更新日期: 2023-09-27 18:16:01

如果我没有错的话,用C++编写类的传统方法如下(这只是一个示例(:

MyClass.h

// MyClass.h
#ifndef MY_CLASS_H
#define MY_CLASS_H
class MyClass
{
public:
    MyClass();
    MyClass(int);
    void method1(int);
    int method2();
private:
    int field;
};
#endif

MyClass.cpp

// MyClass.cpp 
#include "MyClass.h"
MyClass::MyClass()
{
    field = 0;
}
MyClass::MyClass(int n)
{
    field = n;
}
void MyClass::method1(int n)
{
    field = n;
}
int MyClass::method2()
{
    return field;
}

main.cpp

// main.cpp
#include <iostream>
#include "MyClass.h"
using namespace std;
int main()
{
    MyClass mc;
    mc.method1(2);
    cout << mc.method2();
    return 0;
}

这个项目的传统C#等价物是什么?此外,如果我在上面的例子中描述传统正确的C++时犯了一个错误,请纠正它,以防止混淆未来的读者。

C#文件编写约定

在C#中,所有声明都是内联的,每个成员声明中都包含诸如"private"answers"public"之类的访问说明符;在C#中,publicprivateprotectedinternal是成员上的修饰符,就像static在任一语言中一样:

    public class MyClass
    {
        //  ...
        /// <summary>
        /// The "///<summary>" convention is recognized by the IDE, which
        /// uses the text to generate context help for the member. 
        /// 
        /// As TyCobb notes below, method1 in correct C# idiom is more 
        /// likely to be a property than a method -- and the field "backing"
        /// it would be given the same name, but with the first letter 
        /// lowercased and prefixed with an underscore. 
        /// </summary>
        public int Property1
        {
            get { return _property1; }
            set { _property1 = value; }
        }
        private int _property1 = 0;
        private bool _flag1 = false;
        // Regions are a convenience. They don't affect compilation. 
        #region Public Methods
        /// <summary>
        /// Do something arbitrary
        /// </summary>
        public void Method2()
        {
        }
        #endregion Public Methods
    }

Private是默认设置。请注意,您可以在字段声明中放置初始值设定项。

没有与.h/.cpp区别直接等价的东西,尽管有"分部类"这样的东西,其中给定类的一些成员在一个地方定义,而更多的成员在其他地方定义。通常情况下,如果一个类的一些成员由生成的代码定义,而另一些成员则由手写代码定义,则会执行此操作。

将相关内容(事件处理程序、访问web服务的方法,等等(放在"区域"中是个好主意:

    #region INotifyPropertyChanged Implementation
    // ...declare methods which implement interface INotifyPropertyChanged
    #endregion INotifyPropertyChanged Implementation

Regions不仅仅是一种有趣的注释语法,但也不多:编译器需要#region/#endregion对进行匹配,Microsoft IDE会在空白处给你一个加号来折叠/扩展区域。命名它们是可选的,但这是一个非常好的主意,可以跟踪什么是什么。你可以给它们筑巢,所以你需要跟踪它们。

我显式初始化所有内容,但这是C#隐式初始化字段和变量为该类型的标准默认值:数值类型的默认值为零,任何引用类型的默认为null等。您可以使用default(int)default(string)等为任何给定类型获取该值。

这里有一个链接,显示了C#的编码约定。这是一个很好的起点,但每家公司都有自己的编码约定

using System;
//using ... more stuff as necessary;
public class MyClass
{
    public MyClass()
    {
        // Unlike C++, fields are initialized to zero.
    }
    public MyClass(int n)
    {
         field = n;
    }
    public void method1(int n)
    {
         field = n;
    }
    public int method2()
    {
         return field;
    }
    private int field;
};
using namespace1;
using namespace2;
namespace MyNamespace
{
    public class MyClass
    {
        //Fields at the top. Private is optional as it is private by default.
        private int field;
        //Properties next
        //This actually replaces your Method properties in your example.
        public int Field 
        { 
            get { return field; }
            set { field = value; }
        }
        //If you don't need special logic, you can use an auto property
        //instead of using a backing field.
        public int SomeProperty {get; set;}
        //Constructor if needed. It is optional.
        public MyClass()
        {
        }
        //Methods next
        public void SomeMethod()
        {
           //Do something
        }
    }
}

每个人都有自己的小标准,但这几乎是最基本的。