Change T type int in local window

I cannot change this variable "value" from local or immediate windows. What's wrong? T is not int at runtime? What's wrong?

http://im2.ezgif.com/tmp/ezgif-3735159626.gif

I can't change the value either. I think this is a mistake or something else?

class Program { static void Main(string[] args) { Just<int> just = new Just<int>(); just.Try(5); } } class Just<T> { private T _value; public void Try(T value) { this._value = value; } } 

Besides using this, tried to use an instance of Just, worked fine

enter image description here

+5
source share
2 answers

This is a known mistake in evaluating expressions. There is a GitHub issue tracking this error here

+2
source

This error is "normal". The debugger does not seem to get the value that you enter as T = int , but simply as int (it is written on the debugger that it expects an int ), and then tries to convert your int to T Even if T is of type int , it cannot perform the conversion implicitly (usually you just need to specify your value to eliminate such errors), it can recompile your value on the fly with the same configuration to compile the code. And in this configuration, value is of type T = unknown , not T = int .

I think that changing values โ€‹โ€‹on the fly is not very similar to runtime. Because at runtime, T = int
(Someone who knows the compiler / debugger behavior better than I could explain in detail what is going on here.)


But, as in this method:

 public void Try(int value) { // With _value as T _value = value; } 

As a result, you will receive the same error message (and you will not be able to compile it).

Error CS0029 Unable to implicitly convert type 'int' to 'T'


But if you print this in your debugger as a value (for your method):

 (T)(object)10 // Or whatever your int value is 

Or for the test method above (which you really should avoid) :

 _value = (T)(object)value; 

It works, the value changes. (If your value is not of type T )
You explicitly converted your int to T


Nothing is bad, it's just a standard error, not related to the general conversion error.

0
source

Source: https://habr.com/ru/post/1258303/


All Articles