Doubt about static methods and classes

3

I have a question about methods and static classes given the code below:

    static List<MaterialRCM> mr = new List<MaterialRCM>();

    [Authorize]
    public void AddMaterial(int Qtd, int Id)
    {
        mr.Add(new MaterialRCM(Id, Qtd));
    }

Now, how are static classes and methods not types by reference, if two users use the same functionality, you run the risk of having both users' data in the list? To be clearer, this method is called via Ajax, each time the user adds a material this method is called and adds the Material Id and quantity in the list, and if two users are requesting materials?

    
asked by anonymous 25.08.2014 / 21:42

1 answer

1

Yes, not only if two users but any request made to the server will access exactly the same value.

In general it is not recommended to use static variables with ASP.Net, not only because it has a single value for all requests but also because it can cause competition problems, since the value could be accessed and modified by more of a thread at the same time, if you need to keep values on the server I would say to use some other way, such as Session for example.

    
25.08.2014 / 22:04