Return values

I have a method like below

public List<aspnet_Roles> GetAllRoles() { var rolesList = _dbProfile.aspnet_Roles.ToList(); return rolesList; } 

In this method, all roles from the database (LINQ to SQL) were first restored and assigned to the rolesList variable of type var .

I want to know whether it is better to return a value, instead of assigning it to other variables first and then returning it.

This method is lower than the previous version:

 public List<aspnet_Roles> GetAllRoles() { return _dbProfile.aspnet_Roles.ToList(); } 

Will the above method compile in IL as the same or second version? The second version does not have an unnecessary variable declaration.

+4
source share
2 answers

I am sure (although I have not verified yet) that they will compile with the same IL. The C # compiler is smart.

The advantage of the first version is that it simplifies the breakpoint and checks the value before returning.

The second version, of course, is more concise.

This is for you, which you prefer, there is no objective "best" here.

+2
source

The first method will help you in debugging. Right now, AFAIK, there is no way to see the return value of a method during debugging.

Other than that, there is no difference.

+6
source

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


All Articles