C# Simple Shortcuts: Four Developer Secret Weapons
Are you a lazy developer? If so, this article is just for you!
As a C# developer, you know that writing clean, efficient, and readable code can be satisfying when done correctly. C# provides some handy shortcuts that can make your life easier.
In this article, we’ll explore four of these developer secret weapons that can help you write better code faster.
1. Ternary Operator
In the simplest of terms, a ternary operator is a shorthand way to write an if-else statement in a single line. It’s often used to assign a value to a variable based on a condition.
Sample Code
// If-Else statement for adult age verification.
var age = 25;
if (age >= 18)
{
var message = "You are an adult.";
}
else
{
var message = "You are a minor.";
}
The equivalent expression using a ternary operator would be:
// If-Else statement using a ternary operator.
var age = 25;
var message = (age >= 18) ? "You are an adult." : "You are a minor.";
2. Null-Coalescing Operator
The null-coalescing operator (??) is used to provide a default value when a nullable type is null.
Sample Code
// Checking for a null number using an if-else statement.
var myNumber = null;
if (myNumber == null)
{
var myValue = 0;
}
else
{
var myValue = myNumber;
}
The equivalent expression using null-coalescing operator would be:
// Checking for a null number using null-coalescing operator.
var myNumber = null;
var myValue = myNumber ?? 0;
3. Expression-Bodied Members
Expression-bodied members allows you to define a method or property using a concise expression instead of a full method body. This can make your code more readable and reduce the amount of boilerplate code you need to write.
Sample Code
// This is a method that calculates the area of a circle.
public static double CalculateCircleArea(double r)
{
if (r <= 0)
{
throw new ArgumentException("Bad radius.");
}
var area = Math.PI * r ** 2;
return area;
}
The equivalent expression using an expression-bodied method would be:
// This calculates circle area using expression-bodied members.
public static double CalculateCircleArea(double r)
=> r > 0 ? Math.PI * r ** 2 : throw new ArgumentException("Bad radius");
4. Caller Information Attributes
The caller information attributes are a set of attributes that provide information about the caller of a method.
In the simplest of terms, if you include these three parameters in any method, then Microsoft libraries can display the exact file path, method line number, and method name in your output logs!!
Sample Code
// If using microsoft logging libraries, running this method
// will do nothing to the logging and will be oblivious as to
// when this function was ran.
public void PrintMessage(string message)
=> Console.WriteLine(message);
The equivalent expression using caller information attributes would be:
// Pretend this Console.WriteLine is in line 50
// of file with path "C:/Dev/Medium/PrintingUtilities.cs".
public void PrintMessage(
string message,
[CallerFilePath] string path ="",
[CallerLineNumber] int line = 0,
[CallerMemberName] string name ="")
=> Console.WriteLine($"{message} || {path} :: {line} in {name}.");
// Sample output if using microsoft logging libraries with a message
// of "DUMMY STRING" would output AUTOMATICALLY as follows:
// DUMMY STRING || C:/Dev/Medium/PrintUtilites.cs :: 50 in PrintMessage.
If you made it this far, please let me know if I should do another part by responding to this post and letting me know! This is more so beginners shortcuts, I have much more advanced ones!
Conclusion
In conclusion, these four C# shortcuts are powerful tools that every developer should have in their toolbox. Whether you’re trying to write more efficient code, debug your application, or just make your code more readable, these shortcuts can help you get there faster. So go ahead and start using them in your own code today!