🕵️ SicherheitslückenHak5: Hackers Just Poisoned the Rust Supply Chain | Threat Wire(01.09.2026 um 14:00 Uhr)
🕵️ SicherheitslückenHak5: Hackers Found a Way Into Humanoid Robots | Threat Wire(04.09.2026 um 15:04 Uhr)
🔧 AI Nachrichten Bits und so #1021 (Passwort für Laufwerk)(31.08.2026 um 22:15 Uhr)
🔧 AI Nachrichten Bits und so #1022 (Wie Weißbier)(06.09.2026 um 20:39 Uhr)
🍏 iOS / Mac OSHue-App 6.0 ist da: das sind die Neuerungen(07.09.2026 um 17:21 Uhr)
🕵️ SicherheitslückenHak5: Hackers Just Poisoned the Rust Supply Chain | Threat Wire(01.09.2026 um 14:00 Uhr)
🕵️ SicherheitslückenHak5: Hackers Found a Way Into Humanoid Robots | Threat Wire(04.09.2026 um 15:04 Uhr)
🔧 AI Nachrichten Bits und so #1021 (Passwort für Laufwerk)(31.08.2026 um 22:15 Uhr)
🔧 AI Nachrichten Bits und so #1022 (Wie Weißbier)(06.09.2026 um 20:39 Uhr)
🍏 iOS / Mac OSHue-App 6.0 ist da: das sind die Neuerungen(07.09.2026 um 17:21 Uhr)

🔧 Programmierung 🕛 kürzlich 3 Min Lesezeit
0

20 Shorthand Operators in C#

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

This article explores 20 essential shorthand operators every C# developer should master, complete with examples to understand their practical use.






1. Conditional Operator ? :



Also known as the ternary operator, it evaluates a condition and returns one of two values based on the result.




CODE
int age = 18;
string status = age >= 18 ? "Adult" : "Minor";
Console.WriteLine(status);









2. Null-Conditional Operator (Member Access) ?.



Safely access members of an object that might be null without throwing a NullReferenceException.




CODE
string? name = null;
int? length = name?.Length;
Console.WriteLine(length);









3. Null-Conditional Operator (Element Access) ?[]



Similar to ?. but used for arrays or collections.




CODE
int[]? numbers = null;
int? firstNumber = numbers?[0];
Console.WriteLine(firstNumber);









4. Null-Coalescing Operator ??



Provides a default value if the left-hand operand is null.




CODE
string? name = null;
string displayName = name ?? "Guest";
Console.WriteLine(displayName);









5. Null-Coalescing Assignment Operator ??=



Assigns a value to a variable only if it is null.




CODE
string? name = null;
name ??= "Default Name";
Console.WriteLine(name);









6. Null-Forgiving Operator !



Tells the compiler that a value will not be null, bypassing nullability warnings (use cautiously).




CODE
string? name = null;
int length = name!.Length;









7. Index Operator ^



Access elements from the end of a collection.




CODE
int[] numbers = { 1, 2, 3, 4, 5 };
int last = numbers[^1];
Console.WriteLine(last);









8. Range Operator ..



Creates a range of elements from a collection.




CODE
int[] numbers = { 1, 2, 3, 4, 5 };
int[] subset = numbers[1..4];
Console.WriteLine(string.Join(", ", subset));









9. Expression Body Definition =>



Simplifies method or property definitions.




CODE
public class Calculator
{
public int Add(int a, int b) => a + b;
}

Calculator calc = new Calculator();
Console.WriteLine(calc.Add(3, 5));









10. Type-Testing Operator is



Checks if an object is of a specific type.




CODE
object obj = "Codú";
if (obj is string text)
{
Console.WriteLine(text);
}









11. Type-Testing Negation Operator is not



Ensures an object is not of a specific type.




CODE
object obj = 42;
if (obj is not string)
{
Console.WriteLine("Not a string");
}









12. Type-Casting Operator as



Attempts to cast an object to a specific type, returning null if unsuccessful.




CODE
object obj = "Codú";
string? text = obj as string;
Console.WriteLine(text);









13. Compound Assignment Operators (e.g., +=, -=)



Combines an operation with assignment.




CODE
int x = 5;
x += 3; // Equivalent to x = x + 3
Console.WriteLine(x);









14. Lambda Operator => in LINQ



Defines inline functions for LINQ queries.




CODE
int[] numbers = { 1, 2, 3, 4, 5 };
var evens = numbers.Where(n => n % 2 == 0);
Console.WriteLine(string.Join(", ", evens));









15. Elvis Operator in String Interpolation $"{expr}"



Safely handles null values in interpolated strings.




CODE
string? name = null;
Console.WriteLine($"Hello, {name ?? "Codú"}!");









16. Default Literal default



Initializes a variable with its default value for the given type.




CODE
int number = default;
Console.WriteLine(number);









17. Discard Operator _



Ignores values you do not need.




CODE
(int a, _) = (1, 2);
Console.WriteLine(a);









18. Interpolated Verbatim Strings $@



Combines interpolated and verbatim strings.




CODE
string path = "C:\\Users\\{Environment.UserName}";
Console.WriteLine($@"Path: {path}");









19. Conditional Access with Indexer ?[index]



Combines safe navigation and index access.




CODE
Dictionary<string, int>? scores = null;
int? value = scores?["Codu"];
Console.WriteLine(value);









20. Switch Expression switch



A concise way to return values based on conditions.




CODE
string GetDayType(int day) => day switch
{
1 => "Monday",
2 => "Tuesday",
_ => "Other",
};

Console.WriteLine(GetDayType(1));


Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
Hackers Just Poisoned the Rust Supply Chain | Threat Wire
1 Quelle
Hackers Found a Way Into Humanoid Robots | Threat Wire
1 Quelle
Bits und so #1021 (Passwort für Laufwerk)
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten 20 Shorthand Operators in C#

Thematisch verwandte Begriffe: Shorthand, Operators · 6 Treffer

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...