值类型的使用、字符类型、格式化输出和NULL类型

 一、值类型的使用

using System.Reflection;
using System.Security.AccessControl;

namespace DEMO03变量;
internal class Program
{
    static void Main(string[] args)
    {
        //声明变量的语法格式
        //数据类型 变量名
        //声明了一个存储整数的变量叫做a
        int a;
        //赋值:变量名 = 值
        a = 10;
        //查看变量里面的值
        Console.WriteLine(a);
        a = 20;
        Console.WriteLine(a);

        //a = 3.14;不可以
        //类型 变量的名字
        //类型.MaxValue可以获取能存储的最大值
        //类型.MinValue可以获取能存储的最小值
        byte bymax = byte.MaxValue;
        byte bymin = byte.MinValue;
        //-2^31-2^31-1
        int intmax = int.MaxValue;
        int intmin = int.MinValue;
        
        short shortmax = short.MaxValue;
        short shortmin = short.MinValue;

        long longmax = long.MaxValue;
        long longmin = long.MinValue;

        //变量是一个可变的量 他是可以重新赋值
        int x = 10;
        byte y = 20;
        short c = 12;
        long d = 21;
        //大的存储范围里面可以存储小的存储范围
        //x = y;
        //y = c;
        //c = b;

        int ax = 10;
        byte ay = 12;
        short ac = 13;
        long ab = 14;
        //浮点数
        float aa;
        //在给float赋值的时候如果不在单位f F默认为双精度小数
        aa = 1.1f;
        Console.WriteLine(aa);
        aa = 10;
        Console.WriteLine(aa);
        aa = ax;
        Console.WriteLine(aa);
        aa = ay;
        Console.WriteLine(aa);
        aa = ac;
        Console.WriteLine(aa);
        aa = ab;
        Console.WriteLine(aa);
        //float 是老大
        aa = float.MaxValue;
        Console.WriteLine(aa);
        aa = float.MinValue;
        Console.WriteLine(aa);

        //非基础类型
        //decimal:(金钱小数)存储金钱
        //浮点数
        //1、他要确定有效的范围
        //2、他会有进度损失
        //3、他性能消耗很大
        //4、decimal和其他类型不包含血缘关系
        //它的后面必须加上m或M
        decimal q = 1.1m;
        Console.WriteLine(q);
        q = decimal.MaxValue;
        Console.WriteLine(q);
        q = decimal.MinValue;
        Console.WriteLine(q);
        
        //布尔类型bool
        //判断对错 true bool
        bool w = true;
        Console.WriteLine(w);
        bool qwq = false;
        Console.WriteLine(qwq);


    }
}

二、字符类型

namespace Demo字符类型
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //char 
            //1、它什么值都可以存
            //2、它的值必须写在''里面
            //3、也可以不用写在''里但必须对应ascall码
            char a;
            a = '1';
            Console.WriteLine(a);
            a = '汉';//2字节 16位
            Console.WriteLine(a);
            a = '@';
            Console.WriteLine(a);
            a = 'Q';//1字节 8位
            Console.WriteLine(a);
            a = (char)97;
            Console.WriteLine(a);

            //string
            //值必须写在""
            string b = "wqwq我撒旦@#$%^&";
            Console.WriteLine(b);
            String c = "wqwq我撒旦@#$%^&";
            Console.WriteLine(c);

            //java String
            string ca = new string("aaa");
            Console.WriteLine(ca);


        }
    }
}

三、格式化输出

namespace Demo格式化输出
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //格式化输出
            string c = "中国";
            string a = $"我是{c}人";
            Console.WriteLine(a);
            int age = 20 ;
            string b = $"我的年纪是{age}";
            Console.WriteLine(b);

            //基本类型可以直接输出里面的值
            Console.WriteLine(20);
            Console.WriteLine(20.1);
            Console.WriteLine(true);
            Console.WriteLine('c');
            Console.WriteLine($"我的年纪是{age}");
            int age1 = 22;
            Console.WriteLine("我的年纪是{0},{1}....",age,age1);
            
            //改用什么类型就用什么类型 不要使用object非常消耗性能
            //基类
            object s = 10;
            Console.WriteLine(s);
            s = "a";
            Console.WriteLine(s);
            s = 'c';
            Console.WriteLine(s);
            s = true;
            Console.WriteLine(s);
            double n = 1.1;
            s = n;
            Console.WriteLine(s);
        }
    }
}

四、NULL类型

namespace DEMO_Null类型
{
    internal class Program
    {
        public static int a;//0
        public static bool b;//false
        public static char c;//null
        public static string d;//null
        public static long e;//0
        public static short f;//0
        public static double i;//0
        public static object j;//null
        static void Main(string[] args)
        {
            Console.WriteLine(a);
            Console.WriteLine(b);
            Console.WriteLine(c);
            Console.WriteLine(d);   
            Console.WriteLine(e);
            Console.WriteLine(f);
            Console.WriteLine(i);
            Console.WriteLine(j);
        }
    }
}

namespace Demo_Null
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int ?a;//0
        bool ?b;//false
        char ?c;//空
        string? s;//空
        double ?e;//0
        private void Form1_Load(object sender, EventArgs e)
        {
            //MessageBox.Show(a + "");
            //MessageBox.Show(b + "");
            //MessageBox.Show(c + "");
            //MessageBox.Show(s + "");
            //MessageBox.Show(e + "");

            s = null;
            MessageBox.Show(s);
            s = "";
            MessageBox.Show(s);
            //a = null;//?是可空类型
            //MessageBox.Show(a + "");
            object c = null;
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值