Implementation of generics on class in C #

Source: Internet
Author: User
Using system;using system.collections.generic;using system.linq;using system.text; Namespace consoleapplication1{class Program {static void Main (string[] args) {Genericarr            ay<int> intarray = new genericarray<int> (5);//Instantiate an array of generics for (int i = 0; i < 5; i++)                {Intarray.setitem (i, I * 5);            Console.WriteLine (Intarray.getitem (i));             } Console.WriteLine ();            The same method is used for string arrays: genericarray<char> Chararray = new genericarray<char> (5);                for (int i = 0; i < 5; i++) {Chararray.setitem (i, (char) (i +97));            Console.WriteLine (Chararray.getitem (i));            }} public class Genericarray<t> {private t[] array;            Constructor public Genericarray (int size) {array = new t[size]; }//Read method pubLIC T GetItem (int index) {return array[index];            }//Assignment method public void SetItem (int index, T value) {Array[index] = value; }}}}</t></char></char></int></int>

The above is the implementation of the generic in C # on the class content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.