using System.Collections.Generic;using System;publicclassKata{privatereadonlyStatus _status;privatereadonly Dictionary<Status,string> _statusDescriptions =newDictionary<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"}};publicKata(){
_status = Status.Default;}publicKata(Status status){
_status = status;}publicstringGetStatusDescription(){if(!Enum.IsDefined(typeof(Status), _status))thrownewInvalidOperationException();return _statusDescriptions[_status];}}
答案2:
using System;using System.Collections.Generic;publicclassKata{privatereadonlyStatus _status;private Dictionary<Status,string> _statusDescriptions;publicKata(){
_statusDescriptions =newDictionary<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"}};}publicKata(Status status):this(){
_status = status;}publicstringGetStatusDescription(){if(!_statusDescriptions.ContainsKey(_status)){thrownewInvalidOperationException("Invalid status encountered");}return _statusDescriptions[_status];}}
答案3:
using System;using System.Collections.Generic;publicclassKata{privatereadonlyStatus _status;privatereadonly Dictionary<Status,string> _statusDescriptions =newDictionary<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"}};publicKata(){}publicKata(Status status){
_status = status;}publicstringGetStatusDescription(){try{return _statusDescriptions[_status];}catch(Exception ex){thrownewInvalidOperationException("Invalid status encountered");}}}
答案4:
using System;using System.Collections.Generic;publicclassKata{privatereadonlyStatus _status;privatereadonly Dictionary<Status,string> _statusDescriptions =newDictionary<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"}};publicKata(){}publicKata(Status status){
_status = status;}publicstringGetStatusDescription(){stringvalue;if(_statusDescriptions.TryGetValue(_status,outvalue)){returnvalue;}thrownewInvalidOperationException("Invalid status encountered");}}
答案5:
using System;using System.Collections.Generic;publicclassKata{privatereadonlyStatus _status;privatereadonly Dictionary<Status,string> _statusDescriptions =newDictionary<Status,string>();publicKata(){
_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");}publicKata(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");}publicstringGetStatusDescription(){var message ="";foreach(KeyValuePair<Status,string> st in _statusDescriptions){if(st.Key == _status){
message = st.Value;}}if(message ==""){thrownewInvalidOperationException("Invalid status encountered");}return message;}}
答案6:
using System;using System.Collections.Generic;publicclassKata{privatereadonlyStatus _status;privatestaticreadonly Dictionary<Status,string> _statusDescriptions =newDictionary<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"},};publicKata(){}publicKata(Status status){
_status = status;}publicstringGetStatusDescription(){if(!_statusDescriptions.ContainsKey(_status))thrownewInvalidOperationException("Status does not have description");return _statusDescriptions[_status];}}
答案7:
using System;using System.Collections.Generic;publicclassKata{privatereadonlyStatus _status;publicKata(){}publicKata(Status status){
_status = status;}privatereadonly Dictionary<Status,string> _statusDescriptions =newDictionary<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"}};publicstringGetStatusDescription(){if(_statusDescriptions.ContainsKey(_status)){return _statusDescriptions[_status];}thrownewInvalidOperationException("Invalid status encountered");}}
答案8:
using System;using System.Collections.Generic;publicclassKata{privatereadonlyStatus _status;privatereadonly Dictionary<Status,string> _statusDescriptions;publicKata(){
_statusDescriptions =newDictionary<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");}publicKata(Status status):this(){
_status = status;}publicstringGetStatusDescription(){try{return _statusDescriptions[_status];}catch(Exception){thrownewInvalidOperationException();}}}
答案9:
using System;using System.Collections.Generic;publicclassKata{privatereadonlyStatus _status;privatereadonly Dictionary<Status,string> _statusDescriptions;publicKata(){}publicKata(Status status){
_status = status;}publicstringGetStatusDescription(){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:thrownewInvalidOperationException("Invalid status encountered");}}}
答案10:
using System;using System.Collections.Generic;publicclassKata{privatereadonlyStatus _status;privatereadonly Dictionary<Status,string> _statusDescriptions =newDictionary<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"}};publicKata(Status status = Status.Default){
_status = status;}publicstringGetStatusDescription(){string text;if(!this._statusDescriptions.TryGetValue(this._status,out text))thrownewInvalidOperationException();return text;}}