SoFunction
Updated on 2025-04-11

C# Learning Basic Concepts Twenty-Five Questions Page 3/4


What does it mean?
answer:
abstract modifier can be used for classes, methods, properties, events and index indicators (indexer), indicating that it is an abstract member
abstract cannot be used with static  or virtual
Declared as abstract member can not include implementation code, but as long as there are unimplemented abstract members (i.e. abstract classes) in the class, its objects cannot be instantiated. It is usually used to force inheritance classes to implement a certain member.
Example:
using System;
using ;
using ;
namespace Example04
{
#region Base class, abstract class
    public abstract class BaseClass
    {
//Abstract attributes, with both get and set accessors, means that the inheritance class must implement the attribute as readable and writeable
        public abstract String Attribute
        {
            get;
            set;
        }
//Abstract method, pass a string parameter without return value
        public abstract void Function(String value);
//Abstract event, type is a predefined proxy (delegate): EventHandler
        public abstract event EventHandler Event;
//Abstract index indicator, only has a get accessor indicating that the inheritance class must implement the index indicator as read-only
        public abstract Char this[int Index]
        {
            get;
        }
    }
    #endregion
#region Inheritance Class
    public class DeriveClass : BaseClass
    {
        private String attribute;
        public override String Attribute
        {
            get
            {
                return attribute;
            }
            set
            {
                attribute = value;
            }
        }
        public override void Function(String value)
        {
            attribute = value;
            if (Event != null)
            {
                Event(this, new EventArgs());
            }
        }
        public override event EventHandler Event;
        public override Char this[int Index]
        {
            get
            {
                return attribute[Index];
            }
        }
    }
    #endregion
    class Program
    {
        static void OnFunction(object sender, EventArgs e)
        {
            for (int i = 0; i < ((DeriveClass)sender).; i++)
            {
                (((DeriveClass)sender)[i]);
            }
        }
        static void Main(string[] args)
        {
            DeriveClass tmpObj = new DeriveClass();
             = "1234567";
            ();
//Associate the static function OnFunction with the Event event of the tmpObj object
             += new EventHandler(OnFunction);
            ("7654321");
            ();
        }
    }
}
result:
1234567
7
6
5
4
3
2

Previous page1234Next pageRead the full text