栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

C sharp(#)数组定义的简洁方法

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C sharp(#)数组定义的简洁方法

这里以一维整型数组为例

using System;

namespace debug
{
    class Program
    {
        static void Main(string[] args)
        {
        	// 方法一
            int[] numbers = { 2, 12, 5, 15 };
            Console.WriteLine(numbers.GetType());
            foreach(int i in numbers)
            {
                Console.WriteLine($"{i}");
            }
            // 方法二
            int[] numbers1 = new int[] { 2, 12, 5, 15 };
            Console.WriteLine(numbers1.GetType());
            foreach (int i in numbers)
            {
                Console.WriteLine($"{i}");
            }
        }        
    }
}
//Result:
//System.Int32[]
//2
//12
//5
//15
//System.Int32[]
//2
//12
//5
//15

我们可以看到,在定义数组的时候我们可以直接省略后半段的new int[],直接输入想要定义的数组的值即可。

如果大家觉得有用,请高抬贵手给一个赞让我上推荐让更多的人看到吧~

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/954381.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号