Cloning arrays : Array Clone : Data Structure C# Examples


C# Examples » Data Structure » Array Clone »

 

Cloning arrays









    
using  System;
using  System.Collections;
using  System.Collections.Generic;
using  System.Collections.ObjectModel;
using  System.Text;

class  GoodMonthType  {
        private  static  readonly  string[]  monthConstants  =  new  string[]  {
                "January",  "February",  "March",  "April",  "May",  "June",
                "July",  "August",  "September",  "October",  "November",  "December"
        };

        public  string[]  Months
        {
                get  {  return  (string[])monthConstants.Clone();  }
        }

        public  IEnumerable<string>  MonthsEnumerable
        {
                get  {  return  Array.AsReadOnly<string>(monthConstants);  }
        }
}

public  class  MainClass
{

        public  static  void  Main()
        {
                GoodMonthType  mt2  =  new  GoodMonthType();
                
                foreach  (string  m  in  mt2.Months)  {
                        Console.WriteLine(m);
                }
                string[]  months2  =  mt2.Months;
                months2[3]  =  "Not-April";

                foreach  (string  m  in  mt2.Months)  {
                        Console.WriteLine(m);
                }
        }
}
    
   
  
   



Output

January
February
March
April
May
June
July
August
September
October
November
December
January
February
March
April
May
June
July
August
September
October
November
December


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Data Structure
» Array Clone