Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can parameters be constant?

I'm looking for the C# equivalent of Java's final. Does it exist?

Does C# have anything like the following:

public Foo(final int bar); 

In the above example, bar is a read only variable and cannot be changed by Foo(). Is there any way to do this in C#?

For instance, maybe I have a long method that will be working with x, y, and z coordinates of some object (ints). I want to be absolutely certain that the function doesn't alter these values in any way, thereby corrupting the data. Thus, I would like to declare them readonly.

public Foo(int x, int y, int z) {      // do stuff      x++; // oops. This corrupts the data. Can this be caught at compile time?      // do more stuff, assuming x is still the original value. } 
like image 373
Nick Heiner Avatar asked Feb 26 '10 02:02

Nick Heiner


People also ask

Is parameter same as constant?

Variables are usually those that get adjusted on the lowest level, parameters are a level above and constants are those that we don't change or adjust in our current task, but they could be turned into even higher-level parameters (called hyperparameters) if we wanted to further generalize our problem or object.

What does constant parameter mean?

A constant parameter, declared by the keyword const , is a read-only parameter. This means that we can not modify the value of the constant parameter in the function body. Using the const keyword tells the compiler that the value of that parameter will not be changed inside the function.

Do parameters change?

Typically, the value of a statistic can vary from one sample to another, while the parameter remains fixed.

Are parameters just variables?

There is a clear difference between variables and parameters. A variable represents a model state, and may change during simulation. A parameter is commonly used to describe objects statically. A parameter is normally a constant in a single simulation, and is changed only when you need to adjust your model behavior.


2 Answers

Unfortunately you cannot do this in C#.

The const keyword can only be used for local variables and fields.

The readonly keyword can only be used on fields.

NOTE: The Java language also supports having final parameters to a method. This functionality is non-existent in C#.

from http://www.25hoursaday.com/CsharpVsJava.html

EDIT (2019/08/13): I'm throwing this in for visibility since this is accepted and highest on the list. It's now kind of possible with in parameters. See the answer below this one for details.

like image 88
Corey Sunwold Avatar answered Oct 11 '22 19:10

Corey Sunwold


This is now possible in C# version 7.2:

You can use the in keyword in the method signature. MSDN documentation.

The in keyword should be added before specifying a method's argument.

Example, a valid method in C# 7.2:

public long Add(in long x, in long y) {     return x + y; } 

While the following is not allowed:

public long Add(in long x, in long y) {     x = 10; // It is not allowed to modify an in-argument.     return x + y; } 

Following error will be shown when trying to modify either x or y since they are marked with in:

Cannot assign to variable 'in long' because it is a readonly variable

Marking an argument with in means:

This method does not modify the value of the argument used as this parameter.

like image 31
Max Avatar answered Oct 11 '22 21:10

Max