Can parameters be constant?

后端 未结 9 1518
终归单人心
终归单人心 2020-12-24 04:39

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

Does C# have anything like the following:

public Fo         


        
9条回答
  •  天涯浪人
    2020-12-24 05:04

    If struct is passed into a method, unless it's passed by ref, it will not be changed by the method it's passed into. So in that sense, yes.

    Can you create a parameter whose value can't be assigned within the method or whose properties cannot be set while within the method? No. You cannot prevent the value from being assigned within the method, but you can prevent it's properties from being set by creating an immutable type.

    The question isn't whether the parameter or it's properties can be assigned to within the method. The question is what it will be when the method exits.

    The only time any outside data is going to be altered is if you pass a class in and change one of it's properties, or if you pass a value by using the ref keyword. The situation you've outlined does neither.

提交回复
热议问题