Friends.
I may have stumbled upon the greatest feature.
C sharp since introduction of C sharp.
In fact, a feature that has been lingering around.net since.net.
One, one a way that may in fact change how I develop and you develop your
applications forever and is something that you've probably been using for a
decade plus and never knew that's right.
The conditional attribute in C sharp that revolutionizes, how
to conditionally compile code something that is so fundamental.
You use it every time you write debug dot right line today, I'm
gonna break down everything about how I assembled upon the conditional
attribute and why I think it is the most amazing thing in the entire world.
So tune in.
Hey everyone.
I'm James.
And I'm back with another C sharp dev tip.
Why not jump for joy?
Because actually this is the coolest feature in C sharp.
Since I've been using C sharp forever until the next feature of
C sharp, because every feature of C sharp is absolutely amazing, which
is the ABI ability to compile.
Your methods, not conditionally compile code, even though you are,
you would think that you would have to if Def code to conditionally compile
it, but nay, you can actually use a C sharp attribute on any method to
automatically control the compilation.
Of that method.
I stumbled upon this cuz my me and my good friend, Frank Kruger were
recording an episode of our podcast that comes out every week on Monday, merge
conflict, merge conflict FM, go to your podcast app type in merge conflict.
It's us every single Monday.
And I was just sitting there.
I was like, Frank, I'm pretty sure debugged out.
Right line.
Only writes to the Debo log.
In debug.
Right.
And he said, yeah, go to source, bro.
And I was like, okay, right.
Click on this thing, debugged out.
Right line.
Let me go to definition.
Here it is.
And what is this conditional?
Debug that's right.
An attribute that you can add to any void method that enables you to do
conditional compilation, not just for debug, but for any condition.
What we see here is that console do right line.
If I actually go to the definition here on right line.
There's nothing here.
There's no conditions here.
There's this method attribute here, but there is no condition.
It will always get output to the council debugged out right line
on the other hand, right to the debug log only when debugging.
So what that means is if I'm in debug mode and I debug my
application, B, blah, blah B.
And I see it here.
It's actually gonna say hello world.
If I go to view output, what we're gonna see is this is debug.
Let me see if I can make that bigger.
Nope, I can't.
So I'll zoom in over here.
This is debug.
Now, if I go over here, clear this out, toggle it over to release and
run the application again, right?
It's on debug MOS.
So's continue.
What we can see is that we get, no, this is debug because I'm in release.
If I run it just magically over here without anything, we're gonna get the same
exact method calls that we would expect.
Right.
We would expect that we're seeing this is hello world.
No, this is debug.
Anything like that.
Amazing.
Now, when you go into your project settings, you can define.
These compilation symbols.
That's what we're seeing right here.
And by default, a debug and release are automatically defined.
Additionally, we have a net six, a net core wrap, and
so many more inside of here.
So we can define these things automatically.
Now we can also define those automatically inside of.
R source code.
So if I come in here and I was to type in some code a method one and a method,
two, what we can do is add in conditions.
Here's condition one, here's condition, one condition two.
Now this is really cool because this is saying if condition one or
condition two, then it's defined.
So call this method.
Now I can go into my council.
I could write them in here or I could come in and do pound define and
say condition one, just like that.
Now what I'm gonna do is I'm gonna go in and I'm gonna say, uh,
method two.
Now in this case, what we're gonna expect that happens here.
Let's go to define and integer.
There we go.
What we're gonna expect to have happen here is that method one
and method two, get called.
So let's debug it and continue on we're in release mode.
So we see hello.
Hello, world condition one condition one, or condition two.
That's pretty rad.
Now, if I come in and say only define condition two and run this again.
Let's go ahead and close down these and run it again.
There we go.
Sure enough.
Hello world is defined over here, but that first method is not called at all.
Only this one here for condition one or condition two.
So this method exists, but it's gonna get compiled out because there is
no condition that is met in my code.
So what does that mean?
Well, what it means is if you are writing applications and you need to have debug
release, you need to maybe have different.
Sort of configurations for maybe your QA for your test department, you
can define these different methods.
So you never have to write if Def ever again.
That's right.
Never write if Def ever again, you can define these attributes better yet.
You can use these with predefined conditions inside of cross platform apps.
For example, with Donna.
So check this out.
If I go ahead and open up visual studio, I have a.net Maui application.
That's gonna target iOS, Android, Mac, and windows.
Now, if I come into this code and say, uh, if I can say Android, I can say windows.
I can say iOS.
I can say Mac catalyst.
I have all of those conditions available to me now.
Here's what's cool.
The default application with Don Maui goes ahead and it increments.
A counter good old counter application.
Now what's cool.
Here is I've added an add windows and an add Android method down here.
I'm gonna add a conditional for windows.
That's gonna add five and a conditional.
For Android, that adds 10.
So if this is working and in my mind, if everything is a perfect system where this
conditional works, we should see only the addition of five every time, because
the add Android will be conditionally compiled out of my application.
So let's go ahead and bring it over there.
It is.
Click click, click, click, click incremented by five every time.
That's right.
No more.
If depths ever, if you don't need them, you can use the conditional.
Attribute inside of your methods inside of your.net applications with C sharp.
I think that this may revolutionize how I build my applications,
because I'm doing a lot of logging.
I'm doing a lot of different platform, specific code.
And in this system, I want to have the method available to me.
So I don't have to write that IFF around it.
And yet when I go and I call the method.
It will automatically note based on if the conditions are met,
whether to execute that code or not.
I love this feature.
Maybe you've known about this feature for the last 15, 20 plus years.
I haven't, if you have.
Let me know in the comments below, if you didn't also let me know in the
comments below, I wanna know what you think of this really cool attribute and
how you may use it and your applications.
And if you're a library developer, this also may revolutionize how you
specifically are writing your libraries.
Now let me know what you think.
And if you have any questions, I'll let me know in the show notes below.
And I will additionally link to the documentation that outlines every
single piece around the conditional a.
That's gonna do for this video, make sure you jam that like button.
And of course hit subscribe because I put up videos every
single week, right around YouTube.
Thank so much for watching.
Have a.