Ejemplo n.º 1
0
 static void ImmutableValue()
 {
     ArrayAndIndexer<int> aai = new ArrayAndIndexer<int>();
     aai.TheArray[10] = 42;
     Console.WriteLine(aai[10]);
     aai[20] = 99;
     Console.WriteLine(aai.TheArray[20]);
 }
Ejemplo n.º 2
0
        static void MutableValue()
        {
            ArrayAndIndexer<CanChange> aai = new ArrayAndIndexer<CanChange>();
            aai.TheArray[10] = new CanChange { Number = 42 };
            Console.WriteLine(aai[10].Number);
            aai[20] = new CanChange { Number = 99, Name = "独自の要素"};
            Console.WriteLine(aai.TheArray[20].Number);

            aai.TheArray[10].Number = 123;
            Console.WriteLine(aai.TheArray[10].Number);

            //aai[20].Number = 456;
            CanChange elem = aai[20];
            elem.Number = 456;
            Console.WriteLine(aai.TheArray[20].Number);
        }
 /// <summary>
 /// 測試數值型別及參考型別
 /// </summary>
 /// <returns></returns>
 public ActionResult Process6()
 {
     List<CalenderEvent> events = new List<CalenderEvent>();
     List<CalenderEvent> events2 = new List<CalenderEvent>();
     CalenderEvent newEvent = new CalenderEvent()
     {
         Title = "",
         StartTime = new DateTimeOffset(2009, 7, 14, 19, 15, 00, TimeSpan.Zero),
         Duration = TimeSpan.FromHours(1)
     };
     events.Add(newEvent);
     //新增到指定位置
     events.Insert(1, newEvent);
     //將同樣的List加進去
     events.AddRange(events2);
     //使用不存任何元素的陣列模式
     Indexable ix = new Indexable();
     ix[42] = "Xyzzy";
     //使用泛型(T)
     ArrayAndIndexer<int> aai = new ArrayAndIndexer<int>();
     aai.TheArray[10] = 42;
     aai[20] = 99;
     //使用泛型(T)及struct
     ArrayAndIndexer<CanChange> aaq = new ArrayAndIndexer<CanChange>();
     aaq.TheArray[10].Number = 42;
     aaq[20] = new CanChange { Number = 99, Name = "My Item" };
     //因struct是value type,因此無法修改class裡的值
     CanChange elem = aaq[20];
     elem.Number = 456;
     //
     return Content(aaq[10].Number + "_" + aaq[20].Number + "_" + elem.Number);
 }