Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

const vs new const

Tags:

c#

No matter what I Google, there doesn't seem to be an answer for this! In a C# class, what does the new keyword do to a const field?

e.g:

private const int ConstOne = 0;
private new const int ConstTwo = 0;
like image 951
Rob Avatar asked Sep 18 '11 15:09

Rob


People also ask

Why const is used in flutter?

It is recommended to use const constructors whenever possible when creating Flutter widgets. The reason is the performance increase, since Flutter can save some calculations by understanding that it can reuse that widget from a previous redraw in the current one, since it is a constant value.

What is the difference between VAR const and final keywords in Dart?

const means its initial value is must be fixed, can not be a dynamic value; final means its initial value is must be fixed but can be a dynamic value, equal to the var with a fixed value.

What does const {} mean in JS?

The const declaration creates a read-only reference to a value. It does not mean the value it holds is immutable—just that the variable identifier cannot be reassigned. For instance, in the case where the content is an object, this means the object's contents (e.g., its properties) can be altered.

Does const keyword can be set at runtime in Dart?

A variable with the const keyword is initialized at compile-time and is already assigned when at runtime. You can't define const inside a class.


2 Answers

The new keyword is used when hiding a member from a base class.
It doesn't actually do anything; it just tells the compiler not to warn you that you're hiding the base field.

See the documentation

like image 158
SLaks Avatar answered Sep 27 '22 22:09

SLaks


The new keyword is used when hiding a member. It works for all class members like methods, fields, etc.

Basically hiding is used to declare that you know that you are going to hide a member and are aware that it is not going to be used in polymorphic scenarios unlike when you are overriding a member.

Google for hiding vs overriding. For example: http://www.akadia.com/services/dotnet_polymorphism.html

like image 36
Stilgar Avatar answered Sep 27 '22 21:09

Stilgar