Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XS✔ ◾ Update rule.md #8572

Merged
merged 2 commits into from
May 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 10 additions & 21 deletions rules/use-var/rule.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,22 @@ guid: edd8d397-3651-47c0-8737-fa38152558d1

---

::: todo
TODO: Byrden - needs a new home (category) and a complete rewrite
Old content from Better LINQ on .ASPX pasted below
:::

In C# the "var" keyword can be used instead of providing an explicit type when declaring a variable. The type is then inferred from the initial assignment of the variable.
<!--endintro-->
It is just a short hand to save developers from typing out the type of a variable.

Despite what it looks like, the var keyword is not a throwback to the dark ages where we did not have strongly typed variables. It is just a short hand to save developers from typing out the type of a variable.

```csharp
IQueryable<Customers> results =
from c in dbContext.Customers
where c.CompanyName.StartsWith(companyNameTextbox.Text)
select c;
customersBindingSource.DataSource = results;
```
List<string> items = new List<string>();
```

::: bad
Figure: Bad example - You should just use "var" instead of "IQueryable"
Figure: Bad example - You should just use "var" instead of "List<string>"
:::

```csharp
var results =
from c in dbContext.Customers
where c.CompanyName.StartsWith(companyNameTextbox.Text)
select c;
customersBindingSource.DataSource = results;
```
var item = new List<string>();
```

::: good
Figure: Good example - Using "var" to save few keystrokes
Figure: Good example - Using "var" to save a few keystrokes and reduce repetition
:::