在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
此篇为在学习C#基础时,熟悉C#的语法和高级特性的一些小的尝试和笔记,记录一下以供分享 集合初始化器
class MyCollection<T> : IEnumerable { public void Add(T item) { } public IEnumerator GetEnumerator() { throw new NotImplementedException(); } } foreach枚举支持
class MyCollection { public MyEnumerator GetEnumerator() { return new MyEnumerator(); } } class MyEnumerator { public bool MoveNext() { return true; } public int Current { get { return 1; } } } LINQ表达式支持
class MyQuery<T> { public MyQuery<R> Select<R>(Expression<Func<T, R>> func) { throw new NotImplementedException(); } public MyQuery<T> Where(Expression<Func<T, bool>> func) { throw new NotImplementedException(); } } 动态扩展对象
class MyExpando : DynamicObject { private Dictionary<string, object> _items = new Dictionary<string, object>(); // Dynamic View public override IEnumerable<string> GetDynamicMemberNames() { return _items.Keys; } public override bool TryGetMember(GetMemberBinder binder, out object result) { return _items.TryGetValue(binder.Name, out result); } public override bool TrySetMember(SetMemberBinder binder, object value) { _items[binder.Name] = value; return true; } } Async & Await模式支持
namespace WinForm { public partial class Form1 : Form { public Form1() { InitializeComponent(); button1.Enabled = true; button2.Enabled = false; } private async void Form1_Load(object sender, EventArgs e) { await button1; button1.Enabled = false; button2.Enabled = true; await button2; await button2; MessageBox.Show("Hello!"); } } public class ButtonAwaiter : INotifyCompletion { public bool IsCompleted { get { return false; } } public Button Button { get; set; } public void OnCompleted(Action continuation) { EventHandler handler = null; handler = (o, e) => { Button.Click -= handler; continuation(); }; Button.Click += handler; } public void GetResult() { } } public static class ButtonExtensions { public static ButtonAwaiter GetAwaiter(this Button button) { return new ButtonAwaiter { Button = button }; } } } 结语此系列未完待续,希望学习可以持续下去,把它当做一种乐趣,加油! |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论