Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将处理事件处理的代码从VB.NET转换为处理事件处理的C#_C#_Vb.net_Events - Fatal编程技术网

将处理事件处理的代码从VB.NET转换为处理事件处理的C#

将处理事件处理的代码从VB.NET转换为处理事件处理的C#,c#,vb.net,events,C#,Vb.net,Events,我试图理解C#中的事件。 我想将代码从VB.NET转换为C#,但我无法成功。 你能帮我把下面的代码从VB.NET转换成C吗? 我将感谢你的帮助 模块1 类人类 公共事件名称已更改(ByVal aName作为字符串) Public\u名称作为字符串 作为字符串的公共属性名() 收到 返回\u名称 结束 设置(ByVal值作为字符串) _名称=值 RaiseEvent名称已更改(值) 端集 端属性 末级 使用事件p作为PersonClass进行调暗 副标题() p=新的个人等级 p、 Name=“乔

我试图理解C#中的事件。
我想将代码从VB.NET转换为C#,但我无法成功。
你能帮我把下面的代码从VB.NET转换成C吗?
我将感谢你的帮助

模块1
类人类
公共事件名称已更改(ByVal aName作为字符串)
Public\u名称作为字符串
作为字符串的公共属性名()
收到
返回\u名称
结束
设置(ByVal值作为字符串)
_名称=值
RaiseEvent名称已更改(值)
端集
端属性
末级
使用事件p作为PersonClass进行调暗
副标题()
p=新的个人等级
p、 Name=“乔治”
Console.ReadLine()
端接头
子p_nameChanged(ByVal aName作为字符串)处理p.nameChanged
Console.WriteLine(“{0}名称更改为:{1}”,Date.Now.ToString(“dd-MM-yy-hh:MM:ss”),aName)
端接头
端模块

了解C#代表和活动的链接将有助于回答任何问题

下面是模拟VB代码的代码:

using System;

// C# events start with declaring a delegate
public delegate void PersonNameChangeEvent(string oldName, string newName);

public class Person
{
    // I noticed an oddity in the VB code, in that the data member was
    // public.  It should be private, especially since there is a property
    // handler for it
    private string name;
    public string Name
    {
        get { return name;}
        set
        {
            string old = name;
            name = value;

            PersonNameChangeEvent notifyEvent = OnNameChange;
            if (null == OnNameChange)
               return;

            notifyEvent(old, name);
        }
    }

    // a delegate instance is a class member
    public event PersonNameChangeEvent OnNameChange;    
}


public static class Test
{
    public static void Main(string[] args)
    {
         Person p = new Person();

         // assign a method by using of a lamda expression to the event/delegate
         // this expression will get called in the setter of Person.Name....
         // see notifyEvent(old, name)
         p.OnNameChange += ((o, n) => { Console.WriteLine(string.Format("Person '{0}' changed to '{1}'", o, n));  } );

         p.Name = "Bob";
         p.Name = "Joe";
    }
}
不知道你想要什么解释。请提问,我会尽力回答