Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8659667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:04:01+00:00 2026-06-12T16:04:01+00:00

In following code, in the derived class from ArrayList we are using a method

  • 0

In following code, in the derived class from ArrayList we are using a method as OnChanged().

This class is declared as: protected virtual void OnChanged(EventArgs e)

Question: what is the purpose behind protected and virtual keywords?

Anyway it would work but I need to know the purpose behind using protected access modifier and virtual. One last thing this code is from MSDN page teaching how to user delegates to kick an event.

using System;

namespace DelegateForEvents
{
    using System.Collections;

    // A delegate type of hooking up change notificatins.
    public delegate void ChangedEventHandler(object sender, EventArgs e);

    // A class that works just like ArrayList, but sends notifications whevern the list changes.
    public class ListWithChangedEvent : ArrayList
    {
        // An event that clients can use to be notified whenever the elements of the list change
        public event ChangedEventHandler Changed;

        // Invoke the Changed event; called whenever list changes 
        protected virtual void OnChanged(EventArgs e)
        {
            if (Changed != null)
                Changed(this, e);
        }


        // Override some of the methods that can change the list;
        // Invoke event after each
        public override int Add(object value)
        {
            int i = base.Add(value);
            OnChanged(EventArgs.Empty);

            return i;
        }

        public override void Clear()
        {
            base.Clear();
            OnChanged(EventArgs.Empty);
        }

        public override object this[int index]
        {
            //get
            //{
            //    return base[index];
            //}
            set
            {
                base[index] = value;
                OnChanged(EventArgs.Empty);
            }
        }
    }
}


namespace TestEvents
{
    using DelegateForEvents;

    class EventListener
    {
        private ListWithChangedEvent List;

        public EventListener(ListWithChangedEvent list)
        {
            List = list;
            // Add "ListChanged" to the Changed event on "List"
            List.Changed += new ChangedEventHandler(ListChanged);
        }

        // This will be called whenever the list changes.
        private void ListChanged(object sender, EventArgs e)
        {
            Console.WriteLine("This is called when the event fires");
        }

        public void Detach()
        {
            // Death the event and delete the list
            List.Changed -= new ChangedEventHandler(ListChanged);
            List = null;
        }
    }

    class Test
    {

        // Test the ListWithChangedEvent class
        static void Main()
        {
            // Create a new list
            ListWithChangedEvent list = new ListWithChangedEvent();

            // Create a class that listens to list's change event
            EventListener listener = new EventListener(list);

            // Add and remove items from the list.
            list.Add("item 1");
            list.Clear();
            listener.Detach();
            Console.ReadLine();

        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T16:04:02+00:00Added an answer on June 12, 2026 at 4:04 pm

    Virtual: this method is intended to be overridden. When calling this method, generate ‘callvirt’ rather than ‘call’ in the IL.

    Protected: this method is visible to classes which inherit from this one, but is not publicly callable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code: class Base { protected: virtual void methodDefinedInBase() = 0; }
the following code specifies a type MyBase64Binary which is derived from a base class
Consider the following code: class Base { void f() { } }; class Derived:
Following code worked fine abstract class FunctionRunnable<V> implements Runnable { protected abstract V calculate();
consider the following python code class Base(object): def __init__(self): self.foo = 5 class Derived(Base):
Consider the following sample code: #include <iostream> using namespace std; class base { public:
I have the following EF class derived from a database (simplified) class Product {
(This question is a follow-up to C# accessing protected member in derived class )
I wrote the following code: public class A { protected string Howdy = "Howdy!";
I have the following code: using System; using NUnit.Framework; using Rhino.Mocks; public class A

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.