I have the following method but it fails if the string is null. How could I make it just return a null if the string is null?
public static string Truncate(this string value, int maxChars)
{
return value.Length <= maxChars ?
value :
value.Substring(0, maxChars) + " ..";
}
Could someone also explain to me what the “this” is there for. Sorry I’m not so good at C# and this is not my code.
By checking for null and returning appropriately 🙂
Or you could even use another conditional: