gpt4 book ai didi

使用 _ 或这个的私有(private)变量的 c# 样式?

转载 作者:太空宇宙 更新时间:2023-11-03 18:05:35 24 4
gpt4 key购买 nike

<分区>

我想知道在 C# 代码中引用私有(private)字段的首选方式是什么?

在变量 _ 前使用下划线还是使用 this 是首选方式?

这两种风格的优缺点是什么?

在代码中

private string _name;

private void getName()
{
return String.format("{0} - hello", _name);
}

或者第二种方法

private string name;

private void getName()
{
return String.format("{0} - hello", this.name);
}

我更喜欢第二种方法,因为我更清楚这个变量名属于这个实例,我在 Javascript 中广泛使用它们,但我没有看到它们在 C# 中使用,主要是第一种风格

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com