首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

事件处理C#和VB.NET之间的差异

事件处理C#和VB.NET之间的差异主要体现在语法和编程习惯上。以下是两者之间的主要差异:

  1. 语法差异:

C#:

代码语言:csharp
复制
public class EventHandlerExample
{
    public event EventHandler<CustomEventArgs> CustomEvent;

    protected virtual void OnCustomEvent(CustomEventArgs e)
    {
        CustomEvent?.Invoke(this, e);
    }
}

VB.NET:

代码语言:vbnet
复制
Public Class EventHandlerExample
    Public Event CustomEvent As EventHandler(Of CustomEventArgs)

    Protected Overridable Sub OnCustomEvent(e As CustomEventArgs)
        RaiseEvent CustomEvent(Me, e)
    End Sub
End Class
  1. 事件处理器的定义和注册:

C#:

代码语言:csharp
复制
public class EventHandlerExample
{
    public event EventHandler<CustomEventArgs> CustomEvent;

    protected virtual void OnCustomEvent(CustomEventArgs e)
    {
        CustomEvent?.Invoke(this, e);
    }
}

public class CustomEventArgs : EventArgs
{
    public string Message { get; set; }
}

public class EventHandlerExampleUsage
{
    public EventHandlerExampleUsage()
    {
        EventHandlerExample example = new EventHandlerExample();
        example.CustomEvent += Example_CustomEvent;
    }

    private void Example_CustomEvent(object sender, CustomEventArgs e)
    {
        Console.WriteLine(e.Message);
    }
}

VB.NET:

代码语言:vbnet
复制
Public Class EventHandlerExample
    Public Event CustomEvent As EventHandler(Of CustomEventArgs)

    Protected Overridable Sub OnCustomEvent(e As CustomEventArgs)
        RaiseEvent CustomEvent(Me, e)
    End Sub
End Class

Public Class CustomEventArgs
    Inherits EventArgs

    Public Property Message As String
End Class

Public Class EventHandlerExampleUsage
    Public Sub New()
        Dim example As New EventHandlerExample()
        AddHandler example.CustomEvent, AddressOf Example_CustomEvent
    End Sub

    Private Sub Example_CustomEvent(sender As Object, e As CustomEventArgs)
        Console.WriteLine(e.Message)
    End Sub
End Class
  1. 事件处理器的触发:

C#:

代码语言:csharp
复制
public class EventHandlerExample
{
    public event EventHandler<CustomEventArgs> CustomEvent;

    protected virtual void OnCustomEvent(CustomEventArgs e)
    {
        CustomEvent?.Invoke(this, e);
    }

    public void TriggerEvent()
    {
        OnCustomEvent(new CustomEventArgs { Message = "Hello from C#!" });
    }
}

VB.NET:

代码语言:vbnet
复制
Public Class EventHandlerExample
    Public Event CustomEvent As EventHandler(Of CustomEventArgs)

    Protected Overridable Sub OnCustomEvent(e As CustomEventArgs)
        RaiseEvent CustomEvent(Me, e)
    End Sub

    Public Sub TriggerEvent()
        OnCustomEvent(New CustomEventArgs With {.Message = "Hello from VB.NET!"})
    End Sub
End Class

总结:

C#和VB.NET在事件处理方面的差异主要体现在语法上,包括事件定义、事件处理器的注册和触发等。虽然两者在语法上有所不同,但是在事件处理的核心思想上是一致的。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

给自定义控件(Web Control)添加事件的几种方法。前两种方法可以不实现IPostBackEventHandler

写自定义控件已经好久了,也有几个用得时间比较长的,但是对于“事件”一直是比较模糊,没有很详细的理解。     最近升级分页控件,由于原来使用的是VB.net(在VB.net里面添加一个事件是比较容易的),现在想改用C#,而原来的方法又写得比较笨拙,想换一个更简洁一点的方法,所以不得不重新认识一下事件。看了几遍《道不远人深入解析asp.net2.0控件开发》居然没有看懂,可能是自己太笨了吧。又找到了自定义分页控件开发 看了几遍,还是比较迷糊,最后把《自定义分页控件开发》 里提供的代码down下来看

07

事件(Event),绝大多数内存泄漏(Memory Leak)的元凶[下篇] (提供Source Code下载)

在上篇中我们谈到:将一个生命周期较短的对象(对象A)注册到一个生命周期较长(对象B)的某个事件(Event)上,两者便无形之间建立一个引用关系(B引用A)。这种引用关系导致GC在进行垃圾回收的时候不会将A是为垃圾对象,最终使其常驻内存(或者说将A捆绑到B上,具有了和B一样的生命周期)。这种让无用的对象不能被GC垃圾回收的现象,在托管环境下就是一种典型的内存泄漏问题。我们今天将会着重解释其背后的原因。[本篇文章的Source Code从这里下载) 一、CLR垃圾回收简介 在一个托管应用程序中,我们通过不同的方

08
领券