c#基础

掌握C#语言数据类型、深刻理解值类型和引用类型; 掌握流程控制、结构体类型与C的不同之处。
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace lab03
{
    //08 结构体是值类型,即可以使用new操作符分配内存,也可以不使用new操作符分配内存。
    struct node
    {
        public int x;
        public float y;
    }
    class Program
    {
        //02 const常量是在编译时设置其值并且运行期间不能更改其值,readonly常量在程序运行期间只能初始化“一次”。
        const int const_number = 1;
        readonly int readonly_number;
        public Program(){
            readonly_number = 2;
        }
       
        static void Main(string[] args)
        {
            //01 装箱、拆箱是值类型和引用类型间的相互转换。
            int i = 123;
            object box = i;
            int j = (int)box;
      
            //03 匿名类型的变量就是程序员不必指定局部变量的类型,用var来表示类型即可。
            var s = "hello world";
            var t = 100;

            //04 ToString函数能够指定小数点位数格式化一个浮点数。
            float aa = (float)3.1415;
            Console.WriteLine("格式化后的值为:" + aa.ToString("f2"));
            Console.WriteLine();
            //05 switch条件表达式的值和每个case后的常量表达式可以是string类型。
            Console.Write("Please input good or bad:");
            string str = Console.ReadLine();
            switch (str)
            {
                case "good":
                    Console.WriteLine("Excellent!!");
                    break;
                case "bad" :
                    Console.WriteLine("Oh no!");
                    break;
                default:
                    Console.WriteLine("Come on!");
                    break;
            }
            Console.WriteLine();
            //06 string的Split函数可以按照指定的一个或多个分隔符拆分一个字符串,比如把"abc 12;34,56"字符串拆分成四个子串。
            string str1 = "abc 12;34,56";
            string[] str_array = str1.Split(',',' ',';');
            Console.WriteLine("分割后的字符为:");
            Console.WriteLine(string.Join(Environment.NewLine,str_array));
            Console.WriteLine();
            //07 C#二维数组的定义方式和C、Java语法有差异。
            int[,] b = new int[3, 2];
            Console.WriteLine("b的长度为:{0}",b.Length);
            Console.WriteLine();

            //09 C#语言可以使用指针。
            node n1 = new node();//node为结构体
            //node n2; n2.x = 1;
            n1.x = 1;
            n1.y = (float)2.0;
            unsafe {
                node* n2;
                n2 = &n1;
                n2->x = 3;
                Console.WriteLine(n2->x);
            }
            Console.WriteLine();

            //10 Random对象能够产生伪随机数。
            int[] random_num = new int[5];
            Random r = new Random();
            for (int i1 = 0; i1 < random_num.Length; i1++)
            {
                random_num[i1] = r.Next() % 100;//产生0到100的随机数
                Console.WriteLine("这个随机数是:{0}", random_num[i1]);
            }
            Console.WriteLine();
            //11 Stack是泛型类,可以定义存储不同类型元素的栈。
            Stack<string> students_info = new Stack<string>();
            students_info.Push("id:1201030068");
            students_info.Push("name:林源");
            foreach (string info in students_info)
            {
                Console.WriteLine(info);
            }
            Console.WriteLine();
            Console.ReadLine();
        }

    }
}

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值