Alex Rivera | Logout

Passing parameter as final in C#

Asked 2010-04-16T07:16:46.203
15

This might be a duplicate question.But could not find it in search In java to mark a method parameter as constant we declare it as final whats the equivalent C# keyword? Like

public void doSomeThing(final object myObject)
{
//print myobject
}
Edit
Report

2 Answers

3

In C# 4.0 / Visual Studio 2010 with the Code Contracts add-on installed you can use Contract.Invariant in a ContractInvariantMethod attribute to check that a value is not modified during a method call.

Not quite the same thing but about as close as you'll get I think.

answered 2010-04-16T07:30:18.187
1

As others stated, there's no such thing in C#.

If your intention is to forbid the called method to change your object (or know for sure it does not), your only option is to pass a deep clone of your object. - Not very elegant, but the only way possible to be sure.

answered 2010-04-16T07:35:41.637

Your Answer