Java vs. C#

Event Handling

//All event listeners are designed by interface.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

class EventDemo extends JFrame
    public EventDemo()
        JButton clickMe = new JButton();

        setTitle("Event Demo");
        clickMe.setText("Click Me");
        //add event listener
        clickMe.addActionListener(new EventHandler());
        Container con = getContentPane();
        //use layout manager to position a button
        con.setLayout(new FlowLayout());
        setDefaultCloseOperation(DISPOSE_ON_CLOSE );
                                  "I Just Started!");

    //inner class to deal with event listener
    class EventHandler implements ActionListener 
        public void actionPerformed(ActionEvent ae) {
                               "You Clicked My Button!");

    public static void main(String[] args)
        new EventDemo();

//Events and delegates work together
using System;
using System.Drawing;
using System.Windows.Forms;

// custom delegate
public delegate void StartDelegate();

class EventDemo : Form
    // custom event
    public event StartDelegate StartEvent;

    public EventDemo()
        Button clickMe = new Button();

        clickMe.Parent = this;
        clickMe.Text = "Click Me";
        clickMe.Location = new Point(
            (ClientSize.Width - clickMe.Width) /2,
            (ClientSize.Height - clickMe.Height)/2);

        // an EventHandler delegate is assigned
        // to the button's Click event
        clickMe.Click += new EventHandler(OnClickMeClicked);

        // our custom "StartDelegate" delegate is assigned
        // to our custom "StartEvent" event.
        StartEvent += new StartDelegate(OnStartEvent);

        // fire our custom event

    // this method is called when the "clickMe" button is pressed
    public void OnClickMeClicked(object sender, EventArgs ea)
        MessageBox.Show("You Clicked My Button!");

    // this method is called when the "StartEvent" Event is fired
    public void OnStartEvent()
        MessageBox.Show("I Just Started!");

    static void Main(string[] args)
        Application.Run(new EventDemo());