Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Use discard '_'

Tags:

c#

.net

I'm not quite sure of the different between

DataTable itemTable = new DataTable();
itemTable = //CODE

and

_ = new DataTable();
DataTable itemTable = //CODE

Both worked exactly the same, why would I need to change it to an _ like shown?

IMAGE OF THE "POTENTIAL FIX"

enter image description here

like image 628
Harrison Howard Avatar asked Mar 31 '20 08:03

Harrison Howard


Video Answer


1 Answers

Because you have not used the value new DataTable() after assigning, the intellisense thought that you won't need it, so just made the leftside wildcard.

Its just like:

int a = 5;
a = 6; // you didn't even used the value 5 assigned above!

But, in case that the constructor of DataTable has side-effect, the intellisense suggested you not to discard the entire statement, using wildcard _. That's what happened.

If the constructor of DataTable has no side-effect, you are free to remove the statement, just like the above int example.

like image 110
MyBug18 Avatar answered Oct 13 '22 01:10

MyBug18