This method has three parameters. If the extension is not null, for each item in list try to add extension to main text in a way that length of result doesn't exceed 20.I wrote this code, but I don't like its style. I like to use Linq and newer styles for doing this, in order to make it shorter.public string AddExtension(IList<string> extension, bool needDefaultAdd, string text ){    if (needDefaultAdd)    {        if (extension==null)        {            string tempText = text + deafult extension txt;            if (text.Length >20)            {                return text;            }            else            {                return tempText;            }        }        else        {             string finalResult = null;            foreach (var e in extension)            {                string tempText = text + deafult extension txt;                if (text.Length > 20)                {                    finalResult=text;                }                var temp2Text = tempText +e;                if (text.Length > 20)                {                    finalResult=tempText;                }                else                {                    finalResult = finalResult + temp2Text;                }            }            return finalResult;        }    }    return text;}
Checking string length limitation and add items to it in a shorter way with Linq
c#;linq
I don't see any way how linq could help to make this code any shorter or more readable. That being said let us focus on the code.  Like @Caridorc already had mentioned you should place a guard clause at the top of your method to reduce the horizontal spacing.  Another thing is that you check for each item in extension if text.Length > 20 two times which should be done outside of the loop, because if these checks evaluate to true the items of extension won't be used.  If we take a closer look at what is happening inside the loop, we also see that the first check for text.Length > 20 is superflous because the assigned value to finalResult is overwritten a few lines below.  string tempText = text + deafult extension txt;if (text.Length > 20){    finalResult = text;}var temp2Text = tempText + e;if (text.Length > 20){    finalResult = tempText;}else{    finalResult = finalResult + temp2Text;}So we can place a guard clause in front of the loop like  if (text.Length > 20){    return text + deafult extension txt;}foreach (var e in extension){    ....and if we now take a closer look at the condition  if (extension == null){    string tempText = text + deafult extension txt;    if (text.Length > 20)    {        return text;    }    else    {        return tempText;    }}  we can see that the returned value will be text if text.Length > 20 and if extension != null the returned value will be text +  deafult extension txt; if text.Length > 20 which might be a bug in your code.  From this condition I assume that if text.Length > 20 the text parameter should be returned and otherwise some string should be appended to it. Therefor we can use this as a guard condition too which can be placed outside of the if..else and will result in  public string AddExtension(IList<string> extension, bool needDefaultAdd, string text){    if (!needDefaultAdd || text.Length > 20) { return text; }    if (extension == null)    {        return text + deafult extension txt;    }    else    {        string finalResult = string.Empty;        foreach (var e in extension)        {            string tempText = text + deafult extension txt;            var temp2Text = tempText + e;            finalResult = finalResult + temp2Text;        }        return finalResult;    }}  which can be improved by using [string.Join][1] instead of the loop. By eleminating the loop we will also remove this var e which by default is a bad name for a variable because of two reasons. The first reason is that single letter variables don't tell anything about what they are. Using meaningful names for variables will make the code more readable. The second reason is that there exists a widely used variable e which stands for an EventArgs object.  The final result will be  public string AddExtension(IList<string> extension, bool needDefaultAdd, string text){    if (!needDefaultAdd || text.Length > 20) { return text; }    string extendedExtension = text + deafult extension txt;    if (extension == null)    {        return extendedExtension;    }    else    {        return extendedExtension + string.Join(extendedExtension , extension);    }}  which produces the exact same result as the original method including the spelling error of deafult except for the mentioned potential bug.  You could also omit the else.As a side note, if you don't have unit tests for your code you really should add some. For refactoring issues this is just a have to.