Specific programming tip

Posted by: Adam Father 08.10.2024 13:17
Post: The 24. post
Title: C# tip - Prefer var for Local Variables When the Type is Obvious
Language: C#
Description:
Code Example:
Instead of writing:

List<string> names = new List<string>();

You can write:

var names = new List<string>();

The type (List<string>) is clear from the context, so using var here reduces duplication without losing readability.

When not to use var:
Avoid using var when the type isn’t clear from the right-hand side. For example:

var result = GetSomething(); // What type is result?

In such cases, explicitly declaring the type improves code clarity.