简体   繁体   English

增加c#中属性的setter方法的计数

[英]Increase the count in setter method for a property in c#

In a class I have declared one property like below 在一个class我宣布了一个如下所示的property

class MyClass
{
    public string TName
    {
         get {return "";}
    }

    // and some other properties ....
}

One method is returning the type IEnumerable<MyClass> , here I want to get the TName value as 一种方法是返回类型IEnumerable<MyClass> ,这里我想得到TName

Name 1, Name 2, Name 3, Name 4........

based on the count. 根据计数。

Problem: 问题:

How can I increment the value of counter in my setter method of the above property, so that i can append like "Name" + counter; 如何在我的上述属性的setter方法中增加 counter的值,以便我可以追加"Name" + counter;

Or is there any other better way to achieve this without looping and fetching the count from DB . 或者有没有其他更好的方法来实现这一点,而无需looping和从DB fetching计数。

Thank you in advance. 先感谢您。

Same idea (we have a counter s_Count ) but in case you want it thread safe we have to increment it in a special way: 同样的想法(我们有一个计数器s_Count ),但如果你想要它的线程安全,我们必须以一种特殊的方式增加它:

class MyClass {
  private static int s_Count;

  public string TName {
    get;
  }

  public MyClass() {
    TName = $"Name {Interlocked.Increment(ref s_Count)}";
  }    
}

You need a static counter within MyClass that contains the number of instances that were already created: 您需要MyClass中的静态计数器,其中包含已创建的实例数:

class MyClass
{
    static int count = 0;
    public MyClass() { count++; }
}

Now you can easily access that counter within your Name -property: 现在,您可以在Name -property中轻松访问该计数器:

string Name { get => $"Name{ counter }"; }

If there are multple threads that may concurrently increment the counter it´s better to use Interlocked.Increment(ref count) instead of count++ within the constructor. 如果有多个线程可以同时递增counter最好在构造函数中使用Interlocked.Increment(ref count)而不是count++

you'll need to store the value of TName in a private string; 你需要将TName的值存储在私有字符串中;

private string m_TName;
public string TName
{
    get {return m_TName + counter;}
    set {
        if (m_TName != value){
            m_TName = value
        }
    }
}

if TName is always the same you can use 如果TName总是相同的你可以使用

private string m_TName = "Default value";
public string TName
{
    get {return m_TName + counter;}
}

if you want to increment the counter each call 如果你想增加每个电话的计数器

private string m_TName = "Default value";
public string TName
{
    get { 
        counter++;
        return m_TName + counter;
    }
}

if you want it on every instance as per HimBromBeere's comment 如果你想按照HimBromBeere的评论在每个实例上使用它

private string m_TName;
public string TName
{
    get {return m_TName;}
    set {
        if (m_TName != value){
            counter++;
            m_TName = value + counter;
        }
    }
}

if you want it on every instance as per HimBromBeere's comment AND you only want to set it once 如果你想按照HimBromBeere的评论在每个实例上使用它,你只需要设置一次

private string m_TName;
public string TName
{
    get {return m_TName;}
    set {
        if (m_TName != value && m_TName == null){
            counter++;
            m_TName = value + counter;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM