The meaning of the abstract modifier

Source: Internet
Author: User
Tags abstract modifier

The abstract modifier can be used for classes, methods, properties, events, and index indicators (indexer) to represent abstract members

Abstract cannot be used with static or virtual

Declaration as an abstract member can not include implementation code, but as long as there are not implemented abstract members (that is, abstract classes) in the class, its objects cannot be instantiated, and are often used to enforce that an inheriting class must implement a member

Example:

using System;


using System.Collections.Generic;


using System.Text;


namespace Example04


{


#region base class, abstract class


public abstract class BaseClass


  {

The
//abstract property, which has both a get and set accessor, indicates that the inheriting class must implement the property as read-write


public abstract String Attribute


    {


get;


set;


    }


//abstract method, passing in a string parameter with no return value


public abstract void Function (String value);


//abstract event, type is a system-predefined agent (delegate): EventHandler


public abstract event EventHandler event;


//Abstract index indicator, only has a get accessor to indicate that the inheriting class must implement the index indicator as read-only


Public abstract Char This[int Index]


    {


get;


    }


  }


#endregion


#region Inheriting 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). Attribute.length; i++)


      {


Console.WriteLine ((deriveclass) sender) [i]);


      }


    }


static void Main (string[] args)


    {


Deriveclass tmpobj = new Deriveclass ();


Tmpobj.attribute = "1234567";


Console.WriteLine (Tmpobj.attribute);


//associating the static function Onfunction with event events for Tmpobj objects


Tmpobj.event + = new EventHandler (onfunction);


tmpobj.function ("7654321");


Console.ReadLine ();


    }


  }


}

Results:

1234567

7

6

5

4

3

2

1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.