<legend id='Qwf16'><style id='Qwf16'><dir id='Qwf16'><q id='Qwf16'></q></dir></style></legend>

      <bdo id='Qwf16'></bdo><ul id='Qwf16'></ul>

    <small id='Qwf16'></small><noframes id='Qwf16'>

      <i id='Qwf16'><tr id='Qwf16'><dt id='Qwf16'><q id='Qwf16'><span id='Qwf16'><b id='Qwf16'><form id='Qwf16'><ins id='Qwf16'></ins><ul id='Qwf16'></ul><sub id='Qwf16'></sub></form><legend id='Qwf16'></legend><bdo id='Qwf16'><pre id='Qwf16'><center id='Qwf16'></center></pre></bdo></b><th id='Qwf16'></th></span></q></dt></tr></i><div id='Qwf16'><tfoot id='Qwf16'></tfoot><dl id='Qwf16'><fieldset id='Qwf16'></fieldset></dl></div>
    1. <tfoot id='Qwf16'></tfoot>

      带有委托的 C# 观察者/可观察的超级简单示例

      时间:2023-11-11

        <bdo id='6F9A3'></bdo><ul id='6F9A3'></ul>
              <tbody id='6F9A3'></tbody>

            <small id='6F9A3'></small><noframes id='6F9A3'>

          • <legend id='6F9A3'><style id='6F9A3'><dir id='6F9A3'><q id='6F9A3'></q></dir></style></legend>
            <tfoot id='6F9A3'></tfoot>
            <i id='6F9A3'><tr id='6F9A3'><dt id='6F9A3'><q id='6F9A3'><span id='6F9A3'><b id='6F9A3'><form id='6F9A3'><ins id='6F9A3'></ins><ul id='6F9A3'></ul><sub id='6F9A3'></sub></form><legend id='6F9A3'></legend><bdo id='6F9A3'><pre id='6F9A3'><center id='6F9A3'></center></pre></bdo></b><th id='6F9A3'></th></span></q></dt></tr></i><div id='6F9A3'><tfoot id='6F9A3'></tfoot><dl id='6F9A3'><fieldset id='6F9A3'></fieldset></dl></div>

              • 本文介绍了带有委托的 C# 观察者/可观察的超级简单示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我最近开始深入研究 C#,但我一生无法弄清楚在语言中实现观察者/可观察模式时委托是如何工作的.

                I recently started digging into C# but I can't by my life figure out how delegates work when implementing the observer/observable pattern in the language.

                谁能给我一个超级简单的例子来说明它是如何完成的?我用谷歌搜索了这个,但我发现的所有示例要么过于具体,要么过于臃肿".

                Could someone give me a super-simple example of how it is done? I have googled this, but all of the examples I found were either too problem-specific or too "bloated".

                推荐答案

                观察者模式通常使用 事件.

                The observer pattern is usually implemented with events.

                这是一个例子:

                using System;
                
                class Observable
                {
                    public event EventHandler SomethingHappened;
                
                    public void DoSomething() =>
                        SomethingHappened?.Invoke(this, EventArgs.Empty);
                }
                
                class Observer
                {
                    public void HandleEvent(object sender, EventArgs args)
                    {
                        Console.WriteLine("Something happened to " + sender);
                    }
                }
                
                class Test
                {
                    static void Main()
                    {
                        Observable observable = new Observable();
                        Observer observer = new Observer();
                        observable.SomethingHappened += observer.HandleEvent;
                
                        observable.DoSomething();
                    }
                }
                

                有关更多详细信息,请参阅链接文章.

                See the linked article for a lot more detail.

                请注意,上面的示例使用 C# 6 null-conditional 运算符来安全地实现 DoSomething 以处理未订阅 SomethingHappened 的情况,因此为空.如果您使用的是旧版本的 C#,则需要这样的代码:

                Note that the above example uses C# 6 null-conditional operator to implement DoSomething safely to handle cases where SomethingHappened has not been subscribed to, and is therefore null. If you're using an older version of C#, you'd need code like this:

                public void DoSomething()
                {
                    var handler = SomethingHappened;
                    if (handler != null)
                    {
                        handler(this, EventArgs.Empty);
                    }
                }
                

                这篇关于带有委托的 C# 观察者/可观察的超级简单示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:手动创建委托与使用 Action/Func 委托 下一篇:代表:谓词 vs. 动作 vs. Func

                相关文章

                    <bdo id='mF68J'></bdo><ul id='mF68J'></ul>

                    <tfoot id='mF68J'></tfoot>

                  1. <i id='mF68J'><tr id='mF68J'><dt id='mF68J'><q id='mF68J'><span id='mF68J'><b id='mF68J'><form id='mF68J'><ins id='mF68J'></ins><ul id='mF68J'></ul><sub id='mF68J'></sub></form><legend id='mF68J'></legend><bdo id='mF68J'><pre id='mF68J'><center id='mF68J'></center></pre></bdo></b><th id='mF68J'></th></span></q></dt></tr></i><div id='mF68J'><tfoot id='mF68J'></tfoot><dl id='mF68J'><fieldset id='mF68J'></fieldset></dl></div>

                    <small id='mF68J'></small><noframes id='mF68J'>

                    <legend id='mF68J'><style id='mF68J'><dir id='mF68J'><q id='mF68J'></q></dir></style></legend>