Can parameters be constant?

后端 未结 9 1523
终归单人心
终归单人心 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条回答
  •  旧时难觅i
    2020-12-24 05:01

    I know this might be little late. But for people that are still searching other ways for this, there might be another way around this limitation of C# standard. We could write wrapper class ReadOnly where T : struct. With implicit conversion to base type T. But only explicit conversion to wrapper class. Which will enforce compiler errors if developer tries implicit set to value of ReadOnly type. As I will demonstrate two possible uses below.

    USAGE 1 required caller definition to change. This usage will have only use in testing for correctness of your "TestCalled" functions code. While on release level/builds you shouldn't use it. Since in large scale mathematical operations might overkill in conversions, and make your code slow. I wouldn't use it, but for demonstration purpose only I have posted it.

    USAGE 2 which I would suggest, has Debug vs Release use demonstrated in TestCalled2 function. Also there would be no conversion in TestCaller function when using this approach, but it requires a little more of coding of TestCaller2 definitions using compiler conditioning. You can notice compiler errors in debug configuration, while on release configuration all code in TestCalled2 function will compile successfully.

    using System;
    using System.Collections.Generic;
    
    public class ReadOnly
      where VT : struct
    {
      private VT value;
      public ReadOnly(VT value)
      {
        this.value = value;
      }
      public static implicit operator VT(ReadOnly rvalue)
      {
        return rvalue.value;
      }
      public static explicit operator ReadOnly(VT rvalue)
      {
        return new ReadOnly(rvalue);
      }
    }
    
    public static class TestFunctionArguments
    {
      static void TestCall()
      {
        long a = 0;
    
        // CALL USAGE 1.
        // explicite cast must exist in call to this function
        // and clearly states it will be readonly inside TestCalled function.
        TestCalled(a);                  // invalid call, we must explicit cast to ReadOnly
        TestCalled((ReadOnly)a);  // explicit cast to ReadOnly
    
        // CALL USAGE 2.
        // Debug vs Release call has no difference - no compiler errors
        TestCalled2(a);
    
      }
    
      // ARG USAGE 1.
      static void TestCalled(ReadOnly a)
      {
        // invalid operations, compiler errors
        a = 10L;
        a += 2L;
        a -= 2L;
        a *= 2L;
        a /= 2L;
        a++;
        a--;
        // valid operations
        long l;
        l = a + 2;
        l = a - 2;
        l = a * 2;
        l = a / 2;
        l = a ^ 2;
        l = a | 2;
        l = a & 2;
        l = a << 2;
        l = a >> 2;
        l = ~a;
      }
    
    
      // ARG USAGE 2.
    #if DEBUG
      static void TestCalled2(long a2_writable)
      {
        ReadOnly a = new ReadOnly(a2_writable);
    #else
      static void TestCalled2(long a)
      {
    #endif
        // invalid operations
        // compiler will have errors in debug configuration
        // compiler will compile in release
        a = 10L;
        a += 2L;
        a -= 2L;
        a *= 2L;
        a /= 2L;
        a++;
        a--;
        // valid operations
        // compiler will compile in both, debug and release configurations
        long l;
        l = a + 2;
        l = a - 2;
        l = a * 2;
        l = a / 2;
        l = a ^ 2;
        l = a | 2;
        l = a & 2;
        l = a << 2;
        l = a >> 2;
        l = ~a;
      }
    
    }
    

提交回复
热议问题