• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
轻装前行
博客园    首页    新随笔    联系   管理    订阅  订阅
关于C#中数组的练习

代码如下:

 

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

namespace NewConApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,] nums1 = { {1,2,3},{4,5,6} }; //声明一个3列,2行的多维数组

            Console.WriteLine(nums1.Length); //结果为6
            Console.WriteLine(nums1[0,2]);  //结果为3

            int[][] num2 = { 
                            new int[] { 1,2,3,4,5 },
                            new int[] { 2 },
                            new int[] { 4,5 },
                            new int[] { 2,3,9,5 }
                           };  //声明一个元素类型为Int 数组的交错数组

            Console.WriteLine(num2.Length); //结果为4
            Console.WriteLine(num2[3][2]); //结果为9

            int[] num3; //除非声明数组后,马上初始化,否则,必须用 new int[3]
            num3 = new int[3] { 13, 16, 19 }; // 错误写法:  num3 = { 13,16,19 }; 定义好数组后在初始化数值时需要new关键字。

            //错误:int[] nums=new int[3]{};
            //正确:int[] nums=new int[3]{1,2,3}; 初始化变量时如果指定了数组大小必须初始化数据,同时初始化的数据数量和指定的数组长度必须一样。

            string script1 = @"
                <script type='text/javascript'>
                    alert('Hello World');
                </script>
            ";
            Console.WriteLine(script1);

            char a = 'a';  //97
            char f = 'f';  //102
            Console.WriteLine("f-a=" + (f - a));  //结果为5
        }
    }
}

 

谢谢浏览!

posted on 2011-01-05 10:57  轻装前行  阅读(450)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3