C#代码无法理解

use*_*988 -3 c#

我有一个问题,因为我是c ++的编码器,现在我需要阅读一些c#代码.这是命名空间中的一个类,我不明白的是最后一个成员;

public string FilePath
{
            get { return this.filePath; }
            set { this.filePath = value; }
}
Run Code Online (Sandbox Code Playgroud)

我不知道它是成员变量还是成员函数.

如果将其视为成员函数,则应该如此

public string FilePath(***)
{
****;
}
Run Code Online (Sandbox Code Playgroud)

但是这里它没有()类似的参数,它是什么类型的函数?

  class INIFileOperation
    {
    private string filePath;

    [DllImport("kernel32")]
    private static extern long WritePrivateProfileString(string section,
    string key,
    string val,
    string filePath);

    [DllImport("kernel32")]
    private static extern int GetPrivateProfileString(string section,
    string key,
    string def,
    StringBuilder retVal,
    int size,
    string filePath);

    public string ReadAppPath()
    {
        string appPath = Path.GetDirectoryName(Application.ExecutablePath);

        return appPath + "\\Setting.ini";
    }

    public INIFileOperation()
    {
        this.filePath = ReadAppPath();
    }

    public void Write(string section, string key, string value)
    {
        WritePrivateProfileString(section, key, value.ToUpper(), this.filePath);
    }
    public string Read(string section, string key)
    {
        StringBuilder SB = new StringBuilder(255);
        int i = GetPrivateProfileString(section, key, "", SB, 255, this.filePath);
        return SB.ToString();
    }
    public string FilePath
    {
        get { return this.filePath; }
        set { this.filePath = value; }
    }
}
Run Code Online (Sandbox Code Playgroud)

Adi*_*dil 5

这不是方法,但这是c#允许定义类属性的方法.

MSDN 属性是一种成员,它提供了一种灵活的机制来读取,写入或计算私有字段的值.属性可以像它们是公共数据成员一样使用,但它们实际上是称为访问器的特殊方法.这样可以轻松访问数据,并且仍然有助于提高方法的安全性和灵活性.

  • 属性使类能够公开获取和设置值的公共方式,同时隐藏实现或验证代码.
  • get属性访问器用于返回属性值,set访问器用于分配新值.这些访问者可以具有不同的访问级别.
  • value关键字用于定义set访问器分配的值.
  • 未实现set访问器的属性是只读的.
  • 对于不需要自定义访问者代码的简单属性,请考虑使用自动实现属性的选项

    public string FilePath
    {
         get 
         { 
             return this.filePath; 
         }
         set 
         { 
             this.filePath = value; 
         }
    }
    
    Run Code Online (Sandbox Code Playgroud)

您可以在这里这里阅读更多内容