c# - WriteOnly属性还是方法?

标签 c# .net vb.net coding-style

在特定情况下,WriteOnly属性比方法更有意义吗?方法方法对我来说感觉自然得多。

什么是正确的方法?

使用属性:

Public WriteOnly Property MyProperty As String
   Set(ByVal value as String)
      m_myField = value
   End Set
End Property

public string MyProperty
{
   set{ m_myField = value;}
}

使用方法:

Public Sub SetMyProperty(ByVal value as String)
   m_myField = value
End Sub

public void SetMyProperty(string value)
{
   m_myField = value;
}

编辑
为了澄清起见,我指的是“WriteOnly”属性。

最佳答案

我认为属性指示可以是只读或读/写的东西。只写属性的行为并不明显,因此避免创建它们。

例如,在 View 的下拉菜单中设置值列表并访问所选项目:

public interface IWidgetSelector
{
  void SetAvailableWidgets(string[] widgets);

  string SelectedWidget { get; set; }
}

比以下内容更有意义:
public interface IWidgetSelector
{
  string[] AvailableWidgets { set; }

  string SelectedWidget { get; set; }
}

关于c# - WriteOnly属性还是方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/322941/

相关文章:

c# - Coverity 是否支持 xbuild (mono)?

c# - 在使用 Automapper 映射 ViewModel 之后,我应该如何测试以及应该测试什么?

.net - 将命名空间添加到 SyndicationFeed 而不是单个元素?

.net - 我想在 Linux mono 上运行基于 Vb.Net Hello World GUI 的应用程序

.net - 在 vb.net 中读取非常大的文本文件时出现内存不足错误

vb.net - 我在 for 循环中遇到语法问题,从 6 个具有相同名称(末尾的数字除外)的文本框中提取数据

c# - 中间有箭头的画线

c# 鼠标动态面板

c# - 如何使用 LINQ 从没有主键的表中插入/更新/删除记录

c# - JIT 编译器与离线编译器