将变量变为常量

时间:2014-03-19 12:28:30

标签: c# variables constants

是否存在任何使变量变为常量的c#类型/结构? 也许通过设置一个固定值的标志。

示例代码:

foreach (Person p in group)
{
    if (p.Mood.Equals("bad"))
    {
        group.Mood = "bad";
        group.Mood.Fix(); // This should disable any further changing on group.Mood 
    }
    else
    {
        group.Mood = "good";
    }
}

整个集团的情绪应该是"糟糕"如果一个人心情不好。 我知道我可以自己实现这样的东西,但我经常使用它,我可以想象这样的结构已经存在。

我希望你心情愉快! ;)

3 个答案:

答案 0 :(得分:4)

不,变量在声明时变为常量。但是,你可以通过属性设置器获得一些东西:

public string Mood
{
    set 
    {
       if(mood != "Bad")
            mood = value; 
    }
}

答案 1 :(得分:1)

我建议不要这样做,原因如下:

  1. 应该允许设置带有setter的属性
  2. 带有setter的属性不应该只是忽略赋予它的值
  3. 其次,如果您已经确定了最终价值,为什么还要查看收藏中的更多人?

    我会将代码更改为以下三种变体之一:

    group.Mood = "good";
    foreach (Person p in group)
    {
        if (p.Mood.Equals("bad"))
        {
            group.Mood = "bad";
            break;
        }
    }
    

    或者这个:

    string groupMood = "good";
    foreach (Person p in group)
    {
        if (p.Mood.Equals("bad"))
        {
            groupMood = "bad";
            break;
        }
    }
    group.Mood = groupMood;
    

    或者这个:

    if (group.Any(p => p.Mood == "bad"))
        group.Mood = "bad";
    else
        group.Mood = "good";
    

    如果您想要这样的“冰棒”类型,可以尝试使用LINQPad

    void Main()
    {
        var i = new Popsicle<int>(10);
        i.Value = 25;
        i.Value = 33;
        i.Dump();
    
        i.Freeze(FrozenBehavior.IgnoreNewValues);
        i.Value = 17;
        i.Dump();
    
        i = new Popsicle<int>(10);
        i.Freeze(FrozenBehavior.ThrowException);
        i.Value = 17;
        i.Dump();
    }
    
    public class Popsicle<T>
    {
        private T _Value;
        private FrozenBehavior? _FrozenBehavior;
    
        public Popsicle(T value = default(T))
        {
            _Value = value;
        }
    
        public T Value
        {
            get
            {
                return _Value;
            }
    
            set
            {
                if (_FrozenBehavior.HasValue)
                {
                    switch (_FrozenBehavior.GetValueOrDefault())
                    {
                        case FrozenBehavior.IgnoreNewValues:
                            break;
    
                        case FrozenBehavior.ThrowException:
                            throw new InvalidOperationException("This Popsicle<T> has been frozen and configured to throw exceptions if an attempt is made to change its value");
    
                        default:
                            throw new InvalidOperationException("This Popsicle<T> is in an invalid frozen state");
                    }
                }
                else
                    _Value = value;
            }
        }
    
        public void Freeze(FrozenBehavior behavior = FrozenBehavior.ThrowException)
        {
            if (!_FrozenBehavior.HasValue)
                _FrozenBehavior = behavior;
            else
                throw new InvalidOperationException("This Popsicle<T> has already been frozen, cannot re-freeze");
        }
    
        public override string ToString()
        {
            if (!_FrozenBehavior.HasValue)
                return _Value + " (unfrozen)";
            else switch (_FrozenBehavior)
            {
                case FrozenBehavior.IgnoreNewValues:
                    return _Value + " (frozen: ignore)";
    
                case FrozenBehavior.ThrowException:
                    return _Value + " (frozen: throw)";
    
                default:
                    throw new InvalidOperationException("This Popsicle<T> is in an invalid frozen state");
            }
        }
    }
    
    public enum FrozenBehavior
    {
        IgnoreNewValues,
        ThrowException
    }
    

答案 2 :(得分:0)

使用LinQ可以在很短的时间内完成。检查是否有任何心情不好的人,如果是,请将群组心情设置为bad,否则good

group.Mood = group.Any(p => p.Mood == "bad") ? "bad" : "good";