Unity C# const与static readonly的区别与联系

 1 using System;
 2 
 3 namespace Test
 4 {
 5     class MainClass
 6     {
 7         //懒人写法的单例
 8         class Weapon
 9         {
10             public static readonly Weapon Instance;
11             static Weapon()
12             {
13                 Instance=new Weapon();
14             }
15         }
16         class MyWeapon
17         {
18             //static readonly和const的区别
19             public const int Speed=50;//const必须赋值,且只能用这种方法赋值
20             //static readonly 可以赋值也可以不赋值,也可以在用static修饰的构造方法中赋值
21             public static readonly int Speed_1;   
22             public static readonly int Speed_2=50;
23             public static readonly int Speed_3;
24             static MyWeapon()
25             {
26                 Speed_3=50;
27             }
28         }
29 
30         public static void Main (string[] args)
31         {
32             //static readonly和const的联系,
33             //在外边只能通过类名.访问,且在外部不能赋值(二者都是只读的)
34             Console.WriteLine (MyWeapon.Speed_3);//50
35         }
36     }
37 }

 

转载于:https://www.cnblogs.com/Jason-c/p/6655644.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值