C#中数组的使用

C#中数组的使用

1.for

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] friendNames = { "张三","李四","王五" };
            int i;
            Console.WriteLine("我有{0}个朋友!",friendNames.Length);
            for (i = 0;i<friendNames.Length;i++)
            {
                Console.WriteLine(friendNames[i]);
            }

            Console.ReadKey();
        }
    }
}

2.foreach

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] friendNames = { "张三","李四","王五" };
            Console.WriteLine("我有{0}个朋友!",friendNames.Length);
            
            foreach(string friendName in friendNames)
            {
                Console.WriteLine(friendName);
            }

            Console.ReadKey();
        }
    }
}

本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/6728651.html,如需转载请自行联系原作者