zl程序教程

您现在的位置是:首页 >  后端

当前栏目

C#中数组的使用

c#数组 使用
2023-09-27 14:24:30 时间

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();
        }
    }
}