Reputation: 7555
Recently, I came across the Debug.Assert
in C#. However this has been into programming for years but this came across to me as new & quite puzzled me.
Lets say I have C#
method where input field is mandatory like
public class User
{
public class GetUser(string email) // email is required field
{
if(!string.IsNullOrEmptySpace(email)
{
//ToDo
}
}
public class GetUserByEmail(string email) // email is required field
{
Debug.Assert(email != null);
//Assert method takes a boolean value and throws an exception if the value is false
// ToDo
}
}
What is the difference between these 2 approaches or use-case of the second approach?
Should I continue using Debug.Assert lib in .Net Core?
Thanks!
Upvotes: 0
Views: 163
Reputation: 533
Debug.Assert
as you see throws an exception when it receives a false value. We use this to validate developer assumptions. For example: lets say you just coded a function that requires the passed in parameter to be positive. So you want to make sure that no developer on your team passes a negative value. So you would use Debug.Assert
there. However, this line gets completely removed if you build RELEASE
so do NOT use it for business logic. Its just a helper to ensure that you have not forgotten an assumption you made while writing some logic.
Upvotes: 2