using System;

 namespace Test
{
class MainClass
{
//懒人写法的单例
class Weapon
{
public static readonly Weapon Instance;
static Weapon()
{
Instance=new Weapon();
}
}
class MyWeapon
{
//static readonly和const的区别
public const int Speed=;//const必须赋值,且只能用这种方法赋值
//static readonly 可以赋值也可以不赋值,也可以在用static修饰的构造方法中赋值
public static readonly int Speed_1;
public static readonly int Speed_2=;
public static readonly int Speed_3;
static MyWeapon()
{
Speed_3=;
}
} public static void Main (string[] args)
{
//static readonly和const的联系,
//在外边只能通过类名.访问,且在外部不能赋值(二者都是只读的)
Console.WriteLine (MyWeapon.Speed_3);//
}
}
}

Unity C# const与static readonly的区别与联系的更多相关文章

  1. const和static readonly的区别

    我们都知道,const和static readonly的确很像:通过类名而不是对象名进行访问,在程序中只读等等. 在多数情况下可以混用.二者本质的区别在于,const的值是在编译期间确定的,因此只能在 ...

  2. C# const和static readonly区别

    [转]C# const和static readonly区别 以前只是知道Const和static readonlyd的区别在于const的值是在编译期间确定的,而static readonly是在运行 ...

  3. 【转】const和static readonly

    我们都知道,const和static readonly的确很像:通过类名而不是对象名进行访问,在程序中只读等等.在多数情况下可以混用.二者本质的区别在于,const的值是在编译期间确定的,因此只能在声 ...

  4. (C#) What is the difference between "const" and "static readonly" ?

    const int a must be initialized initialization must be at compile time readonly int a can use defaul ...

  5. 到底是 const 还是 static readonly

    真的一样? const 和 static readonly 常在程序中用来声明常量,调用方法也没有什么不同,他们真的一样吗?我们可以做个试验. 程序集内的常量 现在我们建立一个程序,里面有一个MyCl ...

  6. const 还是 static readonly

    到底是 const 还是 static readonly   真的一样? const 和 static readonly 常在程序中用来声明常量,调用方法也没有什么不同,他们真的一样吗?我们可以做个试 ...

  7. const和static readonly 区别

    const的值是在编译期间确定的,因此只能在声明时通过常量表达式指定其值. 而static readonly是在运行时计算出其值的,所以还可以通过静态构造函数来赋值. static readonly ...

  8. c#中const、static、readonly的区别

    1. const与readonly const ,其修饰的字段只能在自身声明时初始化. Readonly 是只读变量,属于运行时变量,可以在类初始化的时候改变它的值.该类型的字段,可以在声明或构造函数 ...

  9. 总结const、readonly、static三者的区别

    const:静态常量,也称编译时常量(compile-time constants),属于类型级,通过类名直接访问,被所有对象共享! a.叫编译时常量的原因是它编译时会将其替换为所对应的值: b.静态 ...

随机推荐

  1. Android--多线程之Handler 前言

    前言 Android的消息传递机制是另外一种形式的“事件处理”,这种机制主要是为了解决Android应用中多线程的问题,在Android中不 允许Activity新启动的线程访问该Activity里的 ...

  2. Spark性能调优之代码方面的优化

    Spark性能调优之代码方面的优化 1.避免创建重复的RDD     对性能没有问题,但会造成代码混乱   2.尽可能复用同一个RDD,减少产生RDD的个数   3.对多次使用的RDD进行持久化(ca ...

  3. 3407: [Usaco2009 Oct]Bessie's Weight Problem 贝茜的体重问题

    3407: [Usaco2009 Oct]Bessie's Weight Problem 贝茜的体重问题 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit:  ...

  4. 最大化最小值 Aggressive cows

    Aggressive cows http://poj.org/problem?id=2456 N间小屋,M头牛,使得牛跟牛之间的距离最远,以防止牛打架. 2<=N<=100000 2< ...

  5. Jmeter BeanShell 时间格式化处理

    工作中碰到的,记录下 在XML格式的请求数据中,Soap接口请求中的日期参数格式是这样的"2016-07-20T18:03:00" 在日和时之间多了一个T 所以在Jmeter--& ...

  6. CentOS 安装mysql-5.7.10(glibc版)

    注:所有shell命令都以root用户执行. 一.下载 shell> cd /home/user/Downloads shell> wget http://mirrors.sohu.com ...

  7. Mac系统安装nginx+rtmp模块

    1.安装命令 ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install) ...

  8. 批量安装python库函数---pip

    废话少说,要的就是干货 pip install -r 文件名 注意如果输入文件名报错,建议使用文件的绝对路径. 作者:marsggbo

  9. Excel 按模板格式导出

    最近遇到一个问题,就是导出数据的时候需要自定义的表头,如图 如果自己用代码写表头的话,可能会有点复杂,而且代码量很多,所以我就想了一个办法,直接在Excel里面把表头定义好,然后把数据写入Excel模 ...

  10. Spark踩坑记——共享变量

    [TOC] 前言 Spark踩坑记--初试 Spark踩坑记--数据库(Hbase+Mysql) Spark踩坑记--Spark Streaming+kafka应用及调优 在前面总结的几篇spark踩 ...