C#练习题答案: 达尔文理论【难度:2级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

mac2025-01-05  3

达尔文理论【难度:2级】:

答案1:

using System; using System.Collections.Generic; public class Creature { private static Dictionary<string, object> creatures = new Dictionary<string, object>(); public object this[string a] { get { return creatures[a]; } set { if(!creatures.ContainsKey(a)) { creatures.Add(a, value); } else { creatures[a] = value; } } } }

答案2:

using System.Collections.Generic; public class Creature { private static readonly Dictionary<string, object> map = new Dictionary<string, object>(); public object this[string key] { get => map[key]; set => map[key] = value; } }

答案3:

using System.Collections.Generic; public class Creature { private static Dictionary<string, object> properties = new Dictionary<string, object>(); public object this[string index] { get { return properties[index]; } set { SetProperty(index, value); } } private void SetProperty(string property, object value) { if (!properties.ContainsKey(property)) { CreateProperty(property, value); return; } properties[property] = value; } private void CreateProperty(string property, object value) { properties.Add(property, value); } }

答案4:

using System.Collections; public class Creature { private static IDictionary properties = new Hashtable(); public object this[string name] { get { return properties[name]; } set { properties[name] = value; } } }

答案5:

using System; using System.Collections.Generic; public class Creature { // Declare an array to store the data elements. private static Dictionary<string, object> arr = new Dictionary<string, object>(); // Define the indexer to allow client code to use [] notation. public object this[string i] { get => arr[i]; set { if (arr.ContainsKey(i)) arr[i] = value; else arr.Add(i, value); } } }

答案6:

using System; using System.Collections; using System.Collections.Generic; using System.Linq; public class Creature : IDictionary<string, object> { private static readonly Dictionary<string, object> _dic = new Dictionary<string, object>(); public Creature() { } public object this[string key] { get => _dic[key]; set => _dic[key] = value; } public ICollection<string> Keys => _dic.Keys; public ICollection<object> Values => _dic.Values; public int Count => _dic.Count; public bool IsReadOnly => true; public void Add(string key, object value) { _dic.Add(key, value); } public void Add(KeyValuePair<string, object> item) { _dic.Add(item.Key, item.Value); } public void Clear() { _dic.Clear(); } public bool Contains(KeyValuePair<string, object> item) { return _dic.Contains(item); } public bool ContainsKey(string key) { return _dic.ContainsKey(key); } public void CopyTo(KeyValuePair<string, object>[] array, int arrayIndex) { throw new NotImplementedException(); } public IEnumerator<KeyValuePair<string, object>> GetEnumerator() { return _dic.GetEnumerator(); } public bool Remove(string key) { return _dic.Remove(key); } public bool Remove(KeyValuePair<string, object> item) { return _dic.Remove(item.Key); } public bool TryGetValue(string key, out object value) { return _dic.TryGetValue(key, out value); } IEnumerator IEnumerable.GetEnumerator() { return _dic.GetEnumerator(); } }

答案7:

using System.Collections.Generic; using System.Linq; public class Creature { private static Dictionary<string, object> properties; private Dictionary<string, object> features = new Dictionary<string, object>(); private static Stack<Creature> creatures; public Creature() { if (properties == null) { properties = new Dictionary<string, object>(); } if (creatures == null) { creatures = new Stack<Creature>(); } if (creatures.Count > 0) { this.features = creatures.Peek().features; } creatures.Push(this); } public object this[string index] { get { object val; features.TryGetValue(index, out val); return val; } set { if (properties.Keys.FirstOrDefault(p => p == index) != null) { properties[index] = value; features[index] = value; } else { properties.Add(index, value); features.Add(index, value); } } } }

答案8:

using System.Collections.Generic; public class Creature { private static Dictionary<string, object> properties = new Dictionary<string, object>(); public object this[string key] { get { object value; properties.TryGetValue(key, out value); return value; } set { if (properties.ContainsKey(key)) properties[key] = value; else properties.Add(key, value); } } }
最新回复(0)