Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anyone found a use of "var" other than for LINQ?

Tags:

c#

linq

Just curious. I'm about 99.999% sure there is none...but anything?

EDIT: These are OK answers (saving typing time or making the code less verbose for "readability"). I guess I should have clarified what I meant by "use" - some construct/design that couldn't be done without "var" .

like image 662
moogs Avatar asked Sep 16 '09 03:09

moogs


People also ask

Is LINQ to SQL still used?

LINQ to SQL was the first object-relational mapping technology released by Microsoft. It works well in basic scenarios and continues to be supported in Visual Studio, but it's no longer under active development.

Is LINQ only for C#?

LINQ (Language Integrated Query) is uniform query syntax in C# and VB.NET to retrieve data from different sources and formats. It is integrated in C# or VB, thereby eliminating the mismatch between programming languages and databases, as well as providing a single querying interface for different types of data sources.

Is LINQ better than SQL?

The main difference between LINQ and SQL is that LINQ is a Microsoft . NET framework component, which adds native data querying capabilities to . NET languages, while SQL is a standard language to store and manage data in RDBMS.

Is LINQ good for performance?

It is slightly slowerLINQ syntax is typically less efficient than a foreach loop. It's good to be aware of any performance tradeoff that might occur when you use LINQ to improve the readability of your code. And if you'd like to measure the performance difference, you can use a tool like BenchmarkDotNet to do so.


1 Answers

Whats better in terms of readability?

AReallyReallyLongBusinessObjectName obj = new AReallyReallyLongBusinessObjectName();

OR

var obj = new AReallyReallyLongBusinessObjectName();

I say in terms of readability because using var has absolutely no impact on your app seeing as the two statements generate the same IL when compiled.

Response to edit: there is nothing that requires you to use var (other than anon types) - its just syntactic sugar.

like image 168
Darko Z Avatar answered Oct 30 '22 18:10

Darko Z