iT邦幫忙

DAY 24
2

C#技術分享系列 第 24

C#技術分享24-建立陣列的方法

常用的建立陣列的方法 有這兩種
先宣告,再直接指定,也可以直接放字串進去,用逗號分隔
方法二用起來比較方便
我比較喜歡方法三 就是 方法二+整齊的排列,這樣如果人家說 第十五家店 後面 要加一個 東區店
那用第二種方法就很不好找了。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //建立陣列 方法一
            string[] weekend = new string[7];
            weekend[0] = "星期一";
            weekend[1] = "星期二";
            weekend[2] = "星期三";
            weekend[3] = "星期四";
            weekend[4] = "星期五";
            weekend[5] = "星期六";
            weekend[6] = "星期日";
            string allday = "";
            foreach (string day in weekend)
            {
                allday += day + ",";    //去掉結尾 逗號
            }
            Console.WriteLine(allday.TrimEnd(','));
            //建立陣列 方法二
            string[] storename0 = { "土城明德店", "台北南昌店", "三峽和平店", "板橋中山店", "北投光明店", "東區地下街", "林口中山店", "基隆仁二店", "桃園中山店", "台北復興店", "台北萬大店", "基隆廟口店", "泰山明志店", "台北信義店", "中壢健行店", "桃園中正店", "台北忠順店", "新竹站前店", "基隆愛買店", "桃園桃鶯店" };
//方法三 整齊的排列
            string[] storename1 ={
"土城明德店",
"台北南昌店",
"三峽和平店",
"板橋中山店",
"北投光明店",
"東區地下街",
"林口中山店",
"基隆仁二店",
"桃園中山店",
"台北復興店",
"台北萬大店",
"基隆廟口店",
"泰山明志店",
"台北信義店",
"中壢健行店",
"桃園中正店",
"台北忠順店",
"新竹站前店",
"基隆愛買店",
"桃園桃鶯店" };


            foreach (string name in storename1)
            {
                Console.Write(name + ",");
            }

            Console.ReadKey();

        }
    }
}

上一篇
C#技術分享23-List泛型集合 範例
下一篇
C#技術分享25-用LINQ挑出有兩欄重覆的資料
系列文
C#技術分享30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言