-1

enter image description here

Basically I want to achieve using a lock to a property that has no private variable, the goal is to have thread safe Properties instead of doing it manually each time.

But not sure how to write that, so the lines aren't red! :)

    public class TestingLOL
    {
        public int Id { get; set; } // Working non private property but is not thread safe.

        public int IdWithLock { get => lock (this); set; } // this is what im trying to achieve without a private property.

        private int shitMethod;
        public int ShitMethod // fuck this way
        {
            get
            {
                ;
                lock (this)
                    return shitMethod;
            }
            set
            {
                ;
                lock (this)
                    shitMethod = value;
            }
        }
    }
Andy x
  • 41
  • 5

1 Answers1

1

For this to work you have to use the name of that backing field

 public int IdWithLock { get => { lock (this){return XXXX;}}; set; }

but that name is not available to you (see nice long discussion here Is it possible to access backing fields behind auto-implemented properties?)

You just gonna have to do it the shty way

pm100
  • 48,078
  • 23
  • 82
  • 145