23

Possible Duplicate:
Read-only (“const”-like) function parameters of C#
Why is there no const member method in C# and const parameter?

Having programmed in C++ in the past, I recall we could make a constant reference/pointer parameter in a method.

If my memory is correct, the below means, that the method cannot alter the reference and the reference itself is a constant reference.

C++ example

void DisplayData(const string &value) const { std::count << value << endl; } 

Is there an equivalent in C# for methods in a class?

The reason why I'm asking is, I'm trying to pass a object by reference (for speed) and at the same time don't want anyone to alter it.

13
  • I swear I saw the same /similar question asked less than 12 hours ago by another user.... Commented Jun 11, 2012 at 14:18
  • You should never pass by reference for performance reasons in C#, it doesn’t work. Most objects are references anyway, passing them by reference has no advantage, and some disadvantages. Commented Jun 11, 2012 at 14:18
  • 2
    Look here - stackoverflow.com/q/3826542/763026 Commented Jun 11, 2012 at 14:19
  • 2
    @KonradRudolph: Passing a reference-type parameter by reference does have advantages on occasion. I don't often use ref, but it can definitely make sense occasionally. Commented Jun 11, 2012 at 14:20
  • 1
    @KonradRudolph: why wouldn't it work? It should certainly provide performance benefits when passing structs. Commented Jun 11, 2012 at 14:22

2 Answers 2

28

Update 16/09/2020

There now appears to be the in parameter modifier that exhibits this behaviour (in essence, a ref readonly). A brief search on when you would ever use this yields the following answer:

Why would one ever use the "in" parameter modifier in C#?

Original Answer

There is no equivalent for C# and it has been asked many, many, many, many times before.

If you don't want anyone to alter the "reference", or perhaps you mean the content of the object, make sure the class doesn't expose any public setters or methods of mutating the class. If you cannot change the class, have it implement an interface that only publicly exposes the members in a read-only fashion and pass the interface reference instead.

If you mean you want to stop the method from changing the reference, then by default if you pass it "by reference", you are actually passing the reference by value. Any attempt from the method to change what the reference points to will only affect the local method copy, not the caller's copy. This can be changed by using the ref keyword on a reference type, at which point the method can point the reference at a new underlying object and it will affect the caller.

Sign up to request clarification or add additional context in comments.

4 Comments

in C# now new you can use in keyword to pass value type as constant
@divyang4481 I had a quick look at the docs and couldn't see any examples of what you mean, can you post something?
Cannot believe that the answer that suggests ref which let change the value is accepted and has highest number of bids so quickly adding quotation from official C# documentation: - in - specifies that this parameter is passed by reference but is only read by the called method. - ref - specifies that this parameter is passed by reference and may be read or written by the called method. - out - specifies that this parameter is passed by reference and is written by the called method.
1

For value types (int, double, byte, char,...,struct) the arguments come in as values and therefore are guaranteed not to affect that calling module.

For string type, although it is a reference type, it is immutable by the CLR, such that nothing you do inside the procedure can affect the original string.

For other reference types (class) there is no way to guarantee changes in the class from the method.

8 Comments

String is not “immutable by the CLR” (whatever that means) but simply by its interface. But the StringBuilder class can modify the underlying String just fine (and yes, it does modify a System.String instance!).
@KonradRudolph: Thanks. I was just going by what MSDN says. msdn.microsoft.com/en-us/library/362314fe.aspx They clearly say the contents cannot be changed after it is created.
@ja72 The difference in wording is rather significant. Strings cannot be modified through their public interface, meaning YOU can't ever modify a string. However, internally, strings can be modified, and in fact they are, but they are only modified in such a way that the modifications are never visible to users (i.e. you). Had you not used the word CLR, but rather, the BCL, your statement would have been correct.
@KonradRudolph While it's true that StringBuilder uses the FastAllocateString() internal call to memcpy the initial memory contents for a new String of a fixed size (as shown here, it does this only for initial construction, which seems less in the spirit of "modify[ing] [an] underlying String just fine..." willy-nilly. In fact I think there is a real hazard here if a string's contents get modified after the lazy init of its so-called GC "sync-block", since it has a cache of the hash code value.
@GlennSlayden I’m not saying that client code can modify a System.String at will, merely that its immutability isn’t a CLR property; as I’m sure you’ll agree there’s no special treatment for this case. The line of code you’ve linked to isn’t as relevant as the lines that just follow it, which perform the actual modification of an allocated string in place, via a fixed pointer to its contents.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.