D
D
Denis Bredun2020-07-08 17:17:45
C++ / C#
Denis Bredun, 2020-07-08 17:17:45

Where in an instance of a primitive reference type, such as string, is the information that this instance is a constant?

This means that where in an instance of a primitive reference type, for example string, is there information that this instance is a constant? How and where is this information about the instance stored, and also where is the information that this instance is private, or public, or internal, or virtual(if we're talking about properties), or readonly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
ayazer, 2020-07-08
@Luffy1

if the field is a constant, all uses of that field will be replaced by the value of the constant at compile time.
original example:

class Program
    {
        static void Main(string[] args)
        {
            const int i1 = 10;
            var k1 = i1 + Func(i1);
            Console.WriteLine(k1);


            var i2 = 10;
            var k2 = i2 + Func(i2);
            Console.WriteLine(k2);
        }

        private static int Func(int i)
        {
            return i * 2;
        }
    }

decompilation result:
internal class Program
  {
    private static void Main(string[] args)
    {
      Console.WriteLine(10 + Program.Func(10));
      int i = 10;
      Console.WriteLine(i + Program.Func(i));
    }

    private static int Func(int i)
    {
      return i * 2;
    }
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question