为什么常量变量与readonly相同 [英] Why constant variables are same as readonly

查看:71
本文介绍了为什么常量变量与readonly相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一般来说,我对常量,只读变量感到困惑,这就是为什么我在google上搜索引用所有链接,无论给定的那些被告知常量,readonly varibles包含固定值但readonly varbles在运行时更改但常量变量不会更改在运行时。在声明.it是常规的intalized是强制性的,但readonly并非强制要求在声明中对varible进行intalize,这只应该在构造函数下进行intalized,但是应该是同样的两个常量



< b>我尝试了什么:



一般来说,我对常量,只读变量感到困惑,这就是为什么我在google上搜索引用所有链接无论给定什么那些被告知是常量,只读变量包含固定值,但在运行时期间只更改了readonly变量,但在运行时期间不会更改常量变量。在声明.it是常规的intalized是强制性的,但readonly并不强制在声明中对varible进行intalize,它只应该在构造函数下进行元素化,但是如何同样的常量

解决方案

它们不一样。

A const 值在编译时定义,永远不能更改。

在编译时未定义 readonly 值 - 它仅在相应的类构造函数中设置,并且每次运行代码时都可以是不同的值。一旦设置,其余代码就无法更改值,因此它是一个可设置常量,允许您(例如)从数据库中读取值而不是在编译时定义它,并将其作为常量从那里处理on。



Quote:

我无法理解readonly





想想看:pi是一个常数 - 3.1415927 ....无论你在宇宙中的哪个位置,你所做的每一次计算都是相同的值申请它。

您的出生日期也是如此 - 它一生都不会改变。但是......你的生日和我的生日不一样,也不会改变。



pi是 const value:你在编译时定义它并且它永远不会改变。

但是dateOfBirth是不同的,你不能改变它,但它是固定的每个人:它是 readonly 因为你在创建一个人的实例时定义了实际值,并且永远不能再改变它。



所以a const 从编译应用程序的那一刻开始修复值,要更改它,你必须重新编译;只有当应用程序运行并在类构造函数中设置值时才修复 readonly 值。



有意义吗?


引用:

我在google上搜索引用所有链接



所以你读过Google给出的198000个答案?你什么也没得到。

你正在接近80个问题,你仍在询问有关C#定义的问题。你打算只在这里提问来学习C#吗?



这是一种计算机语言,如果你有两个不同用法的关键词,可能是因为它们不是同一件事情。该手册包含两个关键字的定义,运气不佳,你会有例子。



你可能应该认真阅读C#手册来学习吗?


Generally I have confused about constant,readonly varibles thats why i had search on google refer all the links whatever the given those are told constant,readonly varibles contain fixed values but readonly varbles are changed during the runtime but constant varibles are not changed during the runtime . constant intalized at declartion .it is mandatory but readonly not mandatory to intalize the varible at declartion that should be intalized under constructor also only but how should be same both constant

What I have tried:

Generally I have confused about constant,readonly varibles thats why i had search on google refer all the links whatever the given those are told constant,readonly varibles contain fixed values but readonly varbles are changed during the runtime but constant varibles are not changed during the runtime . constant intalized at declartion .it is mandatory but readonly not mandatory to intalize the varible at declartion that should be intalized under constructor also only but how should be same both constant

解决方案

They aren't the same.
A const value is defined at compile time and can never be changed.
A readonly value is not defined at compile time - it is set only in the appropriate class constructor and can be a different value each time the code is run. Once set, the rest of the code cannot change the value, so it's a "settable constant" which allows you to (for example) read a value from a database instead of defining it at compile time, and treat it as a constant from there on.

Quote:

I can't understand about readonly



Think about it: pi is a constant - 3.1415927.... it is the same value for every calculation you make, regardless of where in the universe you try to apply it.
So is your date of birth - it never changes for your entire lifetime. But ... your birthday is not the same as mine, which also never changes.

pi is a const value: you define it at compile time and it never changes.
But "dateOfBirth" is different, you can't change it, but it is fixed for each person: it's readonly because you define the actual value when you create an instance of a person, and can never change it again.

So a const value is fixed from the moment you compile your application, and to change it you must recompile; while a readonly value is fixed only when the app runs and sets the value in the class constructor.

Does that make sense?


Quote:

i had search on google refer all the links


So you had read the 198000 answers given by Google ? And you got nothing.
You are approaching 80 questions and you are still asking questions about the definition of C#. Do you plan to learn C# only by asking questions here ?

It is a computer language, if you have 2 keywords with different usages, it is probably because they are not the same thing. The manual contain definition for both keywords, with little luck you will have examples.

May be you should read seriously C# manual to learn it ?


这篇关于为什么常量变量与readonly相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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