01、C#变量和常量

一、变量的概念:变量是随着程序运行,值会发生变化的。

二、变量申明语法:

    方式1:数据类型  变量名

      变量名=数值

string name;
name = "张三"

    方式2:数据类型  变量名 = 数值;

string name = "张三";

    方式3:批量申明

int x=10,y=20;

三、变量初始化

  1. 变量使用必须先初始化。
  2. 变量赋值可以直接赋值。
  3. 使用new关键字赋值。
  4. 变量的作用域,变量上层的花括号就是他的有效范围。
  5. var匿名类型,要获取var是什么类型的变量,用变量名.GetType()
int i = new int();
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace C_shape
{
    class ywj
    {
        public int age = 20;
    }
    class Program
    {
        static void Main(string[] args)
        {
            float i = new float();
            ywj t = new ywj();
            Console.WriteLine("变量i的值为{0},类ywj中的字段age为{1}",i,t.age);
            Console.ReadLine();
        }
    }
}

 

四、常量

  常量指定了其值后,在程序运行中不能再修改,使用const。

const double pi = 3.1415926

 

posted @ 2021-04-26 14:16  遵义枫叶  阅读(101)  评论(0编辑  收藏  举报