c#的变量声明
变量的类型+变量名;
变量:存储在内存中可以改变的量
变量声明的过程:首先会在内存中申请一块可以放心变量对应类型的空间,然后赋值就是把这个值放在申请出来的空间里面。
课里面的比喻很形象,硬盘把数据传给内存,需要的时候内存把数据传给cpu,程序就是保存在内存里面。
常见的几种变量类型
int整型
float单精度浮点型(赋值的时候加个f(例如:1.6f))
double双精度浮点型
string字符串
常量的声明:常量的值是不允许改变的;
const int = 9;
代码:
using System; namespace Lesson04 { class MainClass { public static void Main (string[] args) { // 声明一个整型变量 int level;//level等级 // 对变量进行初始化 level=6; // 值发生变化 level = 7; // 使用变量 Console.WriteLine (level); // 声明一个float类型的变量exp float exp; // 对变量进行初始化 exp = 0.6f; Console.WriteLine (exp); // 声明一个sring类型的变量str string str; // 赋值 str = "你好"; Console.WriteLine (str); // 上述的都是可以改变值的为变量 // 声明并初始化整型常量a const int a=9; // a = 10;常量的值是不允许变化的 Console.WriteLine (a); // 圆周率用常量表示 const float pi=3.141592653579f; Console.WriteLine (pi); } } }
链接:http://edu.csdn.net/course/detail/1982/30920?auto_start=1
转载请注明原文地址: https://ju.6miu.com/read-1122973.html