© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
385 replies
BigggMoustache

How to subscribe an EventHandler? [Answered]

I don't understand how to subscribe this. I don't get it. I honestly don't even know why I used an
EventHandler
EventHandler
instead of an Action or something.
CharberryTree tree = new CharberryTree();
while (true)
tree.MaybeGrow();

public class CharberryTree
{
    public event EventHandler<RipenedEventArgs>? Ripened;

    public CharberryTree()
    {

    }
    private Random _random = new Random();
    public bool Ripe { get; set; }
    public void MaybeGrow()
    {
        // Only a tiny chance of ripening each time, but we try a lot!
        if (_random.NextDouble() < 0.00000001 && !Ripe)
        {
            Ripe = true;
            Ripened?.Invoke(this, new RipenedEventArgs(Ripe));
        }
    }
}

public class Notifier
{
    public Notifier(CharberryTree tree)
    {
        tree.Ripened += OnRipened();
    }
    public void OnRipened()
    {

    }
}

public class Harvester
{
    public Harvester(CharberryTree tree)
    {
        
    }
}

public class RipenedEventArgs : EventArgs
{
    public bool Ripe { get; set; }
    public RipenedEventArgs(bool ripe) => Ripe = ripe;
}
CharberryTree tree = new CharberryTree();
while (true)
tree.MaybeGrow();

public class CharberryTree
{
    public event EventHandler<RipenedEventArgs>? Ripened;

    public CharberryTree()
    {

    }
    private Random _random = new Random();
    public bool Ripe { get; set; }
    public void MaybeGrow()
    {
        // Only a tiny chance of ripening each time, but we try a lot!
        if (_random.NextDouble() < 0.00000001 && !Ripe)
        {
            Ripe = true;
            Ripened?.Invoke(this, new RipenedEventArgs(Ripe));
        }
    }
}

public class Notifier
{
    public Notifier(CharberryTree tree)
    {
        tree.Ripened += OnRipened();
    }
    public void OnRipened()
    {

    }
}

public class Harvester
{
    public Harvester(CharberryTree tree)
    {
        
    }
}

public class RipenedEventArgs : EventArgs
{
    public bool Ripe { get; set; }
    public RipenedEventArgs(bool ripe) => Ripe = ripe;
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements
Next page

Similar Threads

User Control Eventhandler. How to get my control's components to inherit an Eventhandler.
C#CC# / help
3y ago
EventHandler not triggering.
C#CC# / help
3y ago
How to create an entry in Contentful [Answered]
C#CC# / help
4y ago