Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning values

Tags:

c#

I have a method like below

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

In this method first all the roles from database (LINQ to SQL) has been retrieved and assigned to a variable rolesList of type var.

I want to know if returning value directly is better instead of assigning it to other variables first and then returning it.

Is the below method is better than above version:

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

Will the above two methods compile in IL as same or second version is better? Second version doesn't have the unnecessary variable declaration.

like image 933
NCCSBIM071 Avatar asked Feb 26 '23 03:02

NCCSBIM071


1 Answers

First method will help you with debugging. Right now, AFAIK, there is no way to watch return value of a method while debugging.

Other than that, there is no difference.

like image 101
decyclone Avatar answered Mar 07 '23 17:03

decyclone