Member-only story

10 Mind-Blowing C# Hacks

Alex Maher
12 min readApr 24, 2023

--

Hey there! As a passionate C# developer myself, I’ve always been on the lookout for ways to level up my coding skills. I’m excited to share with you some mind-blowing hacks and insights that have helped me become a better programmer.

So, grab a cup of coffee, and join me!

1. Using Caller Information Attributes for Better Debugging and Logging

Caller information attributes allow you to obtain information about the caller of a method, which can be useful for debugging and logging purposes.

public void Log(string message, [CallerMemberName] string memberName = "", [CallerFilePath] string filePath = "", [CallerLineNumber] int lineNumber = 0)
{
Console.WriteLine($"[{memberName}({lineNumber}) in {filePath}]: {message}");
}

In the example above, the ‘Log()’ method uses caller information attributes to automatically include the caller’s member name, file path, and line number in the log message. This makes it easier to trace log messages back to their source without having to manually include this information.

I hope I caught your attention. Before we continue, here’s one interesting fact about C# .NET you might not have known about.

--

--

Alex Maher
Alex Maher

Written by Alex Maher

.NET C# dev with 10+ yrs exp, self-taught & passionate web developer. Sharing tips & experiences in C# and web dev.

Responses (9)