Lädt...


🔧 C# Tip: Use var for Obvious Types, but Prefer Explicit Types for Ambiguous Types


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Let’s talk about the use of var for Obvious Types, a practice that helps reduce code verbosity without sacrificing clarity.

In C#, var allows the compiler to infer the variable type, which reduces the need for repetitive declarations. However, using var indiscriminately can make the code harder to read, especially when the type isn’t immediately clear. Therefore, it’s recommended to use var only when the type is obvious from the expression. In cases where the type isn’t clear (e.g., objects from APIs or complex method calls), it’s better to declare the type explicitly.

This practice helps keep the code concise without sacrificing readability, especially in projects where many developers need to understand the code quickly.

Example:

public class Program
{
    public static void Main()
    {
        // Obvious type, good use of var
        var total = 100;

        // Prefer explicit type to avoid ambiguity
        List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

        // `decimal` with `var` is evident by the value
        var amount = 100.0m;

        Console.WriteLine($"Total: {total}");
        Console.WriteLine($"First number: {numbers[0]}");
        Console.WriteLine($"Amount: {amount}");
    }
}

In the example, we use var for variables where the type is obvious, like int and decimal, simplifying the code. For variables where the type is less obvious (like a list of numbers), we choose to declare the type explicitly. This balance allows for cleaner code without sacrificing clarity.

Source code: GitHub

...

🔧 C# Tip: Use var for Obvious Types, but Prefer Explicit Types for Ambiguous Types


📈 140.96 Punkte
🔧 Programmierung

📰 The obvious and not-so-obvious data you wouldn’t want companies to have


📈 39.34 Punkte
📰 IT Security Nachrichten

🔧 Infer Types to Avoid Explicit Types


📈 31.9 Punkte
🔧 Programmierung

📰 Apple Rebukes Australia's 'Dangerously Ambiguous' Anti-Encryption Bill


📈 30.16 Punkte
📰 IT Security Nachrichten

📰 Audit being asked an ambiguous question


📈 30.16 Punkte
📰 IT Security Nachrichten

🔧 More info on ambiguous 500 errors


📈 30.16 Punkte
🔧 Programmierung

🪟 Device Not Migrated due to Partial or Ambiguous Match [Fix]


📈 30.16 Punkte
🪟 Windows Tipps

🐧 [ask] ZRam on /var/tmp? Compressing /var/tmp, possible?


📈 29.33 Punkte
🐧 Linux Tipps

🕵️ D-Link DSL-2640T cgi-bin/webcm var:RelaodHref/var:conid cross site scripting


📈 29.33 Punkte
🕵️ Sicherheitslücken

🔧 C# Tip: Prefer StringBuilder for Concatenating Multiple Strings


📈 27.95 Punkte
🔧 Programmierung

🔧 C# Tip: Prefer String Interpolation over Concatenation


📈 27.95 Punkte
🔧 Programmierung

🍏 An Apple Watch Ultra for $48? No, but you can get this obvious knockoff.


📈 25.53 Punkte
🍏 iOS / Mac OS

📰 The Great Resignation continues. There's an obvious fix, but many bosses aren't interested


📈 25.53 Punkte
📰 IT Nachrichten

🔧 Why I Prefer 'function' Declarations for Top-Level Symbols (But Won't Use Them Anymore)


📈 25.47 Punkte
🔧 Programmierung

🔧 Raise The Red Flag Early: The Power of Explicit Return Types in TypeScript


📈 25.01 Punkte
🔧 Programmierung

🔧 Dev Tip: How to view Dev Tip: How to view localhost web apps on your phone


📈 24.37 Punkte
🔧 Programmierung

📰 Tumblr Will Now Allow Nudity But Not Explicit Sex


📈 23.97 Punkte
📰 IT Security Nachrichten

🔧 Prefer utility types over model changes in TypeScript


📈 22.66 Punkte
🔧 Programmierung

🔧 Prefer strict types in Typescript


📈 22.66 Punkte
🔧 Programmierung

🔧 I built these 5 types of loading animation in HTML and CSS, which do you prefer?


📈 22.66 Punkte
🔧 Programmierung

🔧 Rust developers can now use explicit disciminants on enums with fields thanks to Rust 1.66 changes


📈 21.97 Punkte
🔧 Programmierung

🕵️ Monero: Potential use-after-free due to struct array_entry_t lacking an explicit copy constructor


📈 21.97 Punkte
🕵️ Sicherheitslücken

🔧 You May Prefer to Know Less About PKI Flaws but Now Is Too Late


📈 21.62 Punkte
🔧 Programmierung

🐧 Linux Users / Gamers - What do you do instead? I hear Ubuntu 20 (I prefer Mint, but w/e) is better?


📈 21.62 Punkte
🐧 Linux Tipps

📰 Cybersecurity pros are using artificial intelligence but still prefer the human touch


📈 21.62 Punkte
📰 IT Security Nachrichten

📰 Did Sam Bankman-Fried Finally Admit the Obvious?


📈 19.67 Punkte
📰 IT Security Nachrichten

🔧 Beyond the Obvious: Uncovering the Hidden Challenges in Cybersecurity


📈 19.67 Punkte
🔧 Programmierung

matomo