C#练习题答案: 重构出一个switch语句的解释【难度:2级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

重构出一个switch语句的解释【难度:2级】:

答案1:

using System.Collections.Generic;
using System;

public class Kata
{
    private readonly Status _status;
    private readonly Dictionary<Status, string> _statusDescriptions = new Dictionary<Status, string>()
    {
        { Status.Default, "I have never been set" },
        { Status.New, "I am new!" },
        { Status.Active, "I am active" },
        { Status.Deactivated, "I have been deactivated" }
    };
    
    public Kata()
    {
        _status = Status.Default;
    }

    public Kata(Status status)
    {
        _status = status;
    }

    public string GetStatusDescription()
    {
        if (!Enum.IsDefined(typeof(Status), _status)) throw new InvalidOperationException();
        return _statusDescriptions[_status];
    }
}

答案2:

using System;
using System.Collections.Generic;
  public class Kata
{
  private readonly Status _status;
  private Dictionary<Status, string> _statusDescriptions;
  public Kata()
  {
    _statusDescriptions = new Dictionary<Status, string>()
    {
      {Status.Default, "I have never been set"},
      {Status.New , "I am new!"},
      {Status.Active, "I am active"},
      {Status.Deactivated, "I have been deactivated"}
    };
  }

  public Kata(Status status) : this()
  {
    _status = status;
  }

  public string GetStatusDescription()
  {
    if(!_statusDescriptions.ContainsKey(_status))
    {
      throw new InvalidOperationException("Invalid status encountered");
    }
    return _statusDescriptions[_status];
  }
}

答案3:

using System;
using System.Collections.Generic;
  public class Kata
  {
      private readonly Status _status;
      private readonly Dictionary<Status, string> _statusDescriptions = new Dictionary<Status, string>()
      {
       {Status.Default, "I have never been set" },
       {Status.New, "I am new!" },
       {Status.Active, "I am active" },
       {Status.Deactivated, "I have been deactivated" }
      };
      public Kata()
      {
      }

      public Kata(Status status)
      {
          _status = status;
      }

      public string GetStatusDescription()
      {
         try
            {
                return _statusDescriptions[_status];
            }
            catch(Exception ex)
            {
                throw new InvalidOperationException("Invalid status encountered");
            }
      }
  }

答案4:

using System;
using System.Collections.Generic; 

  public class Kata
  {
      private readonly Status _status;
      private readonly Dictionary<Status, string> _statusDescriptions = new Dictionary<Status, string>() {
            {Status.Default, "I have never been set"},
            {Status.New, "I am new!"},
            {Status.Active, "I am active"},
            {Status.Deactivated, "I have been deactivated"}
           };

      public Kata()
      {
      }

      public Kata(Status status)
      {
          _status = status;
      }

      public string GetStatusDescription()
      {
        string value;
        if (_statusDescriptions.TryGetValue(_status, out value)) {
          return value;
        }
        
        throw new InvalidOperationException("Invalid status encountered");
      }
  }

答案5:

using System;
using System.Collections.Generic;

  public class Kata
  {
      private readonly Status _status;
      private readonly Dictionary<Status, string> _statusDescriptions = new Dictionary<Status, string>();
      
      public Kata()
      {
            _statusDescriptions.Add(Status.Default, "I have never been set");
            _statusDescriptions.Add(Status.New, "I am new!");
            _statusDescriptions.Add(Status.Active, "I am active");
            _statusDescriptions.Add(Status.Deactivated, "I have been deactivated");
      }

      public Kata(Status status)
      {
          _status = status;
            _statusDescriptions.Add(Status.Default, "I have never been set");
            _statusDescriptions.Add(Status.New, "I am new!");
            _statusDescriptions.Add(Status.Active, "I am active");
            _statusDescriptions.Add(Status.Deactivated, "I have been deactivated");

      }

      public string GetStatusDescription()
      {
            var message = "";
            foreach(KeyValuePair<Status, string> st in _statusDescriptions)
            {
                if (st.Key == _status)
                {
                    message = st.Value;
                }
            }
            if(message == ""){
            throw new InvalidOperationException("Invalid status encountered");
            }
            return message;
  }
  }

答案6:

using System;
using System.Collections.Generic;

  public class Kata
  {
      private readonly Status _status;
      
      private static readonly Dictionary<Status, string> _statusDescriptions = new Dictionary<Status, string>
      {
        { Status.Default, "I have never been set" },
        { Status.New, "I am new!" },
        { Status.Active, "I am active" },
        { Status.Deactivated, "I have been deactivated" },
      };

      public Kata()
      {
      }

      public Kata(Status status)
      {
          _status = status;
      }

      public string GetStatusDescription() 
      {
        if (!_statusDescriptions.ContainsKey(_status)) throw new InvalidOperationException("Status does not have description");
        return _statusDescriptions[_status];
      }
  }

答案7:

using System;
using System.Collections.Generic;

  public class Kata
  {
      private readonly Status _status;

      public Kata()
      {
      }

      public Kata(Status status)
      {
          _status = status;
      }

      private readonly Dictionary<Status, string> _statusDescriptions = new Dictionary<Status, string>
      {
          {Status.Default, "I have never been set"},
          {Status.New, "I am new!"},
          {Status.Active, "I am active"},
          {Status.Deactivated, "I have been deactivated"}
      };
      
      public string GetStatusDescription()
      {
          if (_statusDescriptions.ContainsKey(_status))
          {
              return _statusDescriptions[_status];
          }
          
          throw new InvalidOperationException("Invalid status encountered");
      }
  }

答案8:

using System;
using System.Collections.Generic;

  public class Kata
  {
      private readonly Status _status;
      
      private readonly Dictionary<Status, string> _statusDescriptions;

      public Kata()
      {
         _statusDescriptions = new Dictionary<Status, string>();
         _statusDescriptions.Add(Status.Default, "I have never been set");
         _statusDescriptions.Add(Status.New, "I am new!");
         _statusDescriptions.Add(Status.Active, "I am active");
         _statusDescriptions.Add(Status.Deactivated, "I have been deactivated");
      }

      public Kata(Status status) : this()
      {
          _status = status;
      }

      public string GetStatusDescription()
      {
          try
          {
            return _statusDescriptions[_status];
          
          }
          catch(Exception)
          {
            throw new InvalidOperationException();
          }
      }
  }

答案9:

using System;
using System.Collections.Generic;

  public class Kata
  {
      private readonly Status _status;
      private readonly Dictionary<Status, string> _statusDescriptions;
      public Kata()
      {
      }

      public Kata(Status status)
      {
          _status = status;
      }

      public string GetStatusDescription()
      {
          switch (_status)
          {
              case Status.Default:
                  return "I have never been set";

              case Status.New:
                  return "I am new!";

              case Status.Active:
                  return "I am active";

              case Status.Deactivated:
                  return "I have been deactivated";

              default:
                  throw new InvalidOperationException("Invalid status encountered");
          }
      }
  }

答案10:

using System;
using System.Collections.Generic;

public class Kata
{
    private readonly Status _status;
    private readonly Dictionary<Status, string> _statusDescriptions = new Dictionary<Status, string>
    {
        { Status.Default, "I have never been set" },
        { Status.New, "I am new!" },
        { Status.Active, "I am active" },
        { Status.Deactivated, "I have been deactivated" }
    };

    public Kata(Status status = Status.Default)
    {
        _status = status;
    }

    public string GetStatusDescription()
    {
        string text;
        if (! this._statusDescriptions.TryGetValue(this._status, out text))
           throw new InvalidOperationException();
        return text; 
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值