Vaguely with? operator in c #

I have a variable called full_name if full_name has a string length> 5 I would like to set nm to the first 4 characters of full_name, otherwise I would like to set nm for all characters of full_name.

var nm;

if (full_name.Length > 5)
{
    nm = full_name.Substring(0, 4);
}
else
{
    nm = full_name;
};

Am I completely confused "?" operator.

Can I use it for this?

+5
source share
1 answer
var nm = full_name.Length > 5 ? full_name.Substring(0, 4) : full_name;
+14
source

All Articles