静态变量和const变量之间的区别 [英] Difference between static and const variables

查看:166
本文介绍了静态变量和const变量之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在声明全局变量时,静态和常量之间的区别是什么?

what is the difference between "static" and "const" when it comes to declare global variables;

namespace General
{
    public static class Globals
    {
        public const double GMinimum = 1e-1;

        public const double GMaximum = 1e+1;
    }
}

哪个更好(考虑到这些变量不会

which one is better (considering that these variables wont be changing ever)

namespace General
{
    public static class Globals
    {
        public static double GMinimum1 = 1e-1;

        public static double GMaximum1 = 1e+1;
    }
}


推荐答案

const 和readonly对数据成员执行类似的功能,但是它们有一些重要的区别。
常量成员是在编译时定义的,不能在运行时更改。使用const关键字将常量声明为字段,并且必须在声明常量时对其进行初始化。

const and readonly perform a similar function on data members, but they have a few important differences. A constant member is defined at compile time and cannot be changed at runtime. Constants are declared as a field, using the const keyword and must be initialized as they are declared.

静态修饰符用于声明静态成员,这意味着该成员不再绑定到特定对象。该值属于该类,此外,无需创建该类的实例即可访问该成员。仅存在一个静态字段和事件的副本,并且静态方法和属性只能访问静态字段和静态事件

The static modifier is used to declare a static member, this means that the member is no longer tied to a specific object. The value belongs to the class, additionally the member can be accessed without creating an instance of the class. Only one copy of static fields and events exists, and static methods and properties can only access static fields and static events

这篇关于静态变量和const变量之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆