Simple case:
str = "<html>" + (objName != null ? objName.Name : "") + "</html>";
Little more performant:
if (objName == null)
str = "<html></html>";
else
String.Concat("<html">, objName.Name, "</html>");
If you are doing this a lot, consider using StringBuilder, which might look someting like:
var html = new StringBuilder();
html.Append("<html>");
html.Append(objName != null ? objName.Name : "");
...
html.Append("</html>";
If you are adding several tags together, and not using StringBuilder you need to do someting like this:
str = "<html>" + (objName != null ? objName.Name : "") + "</html>";
str += "<script>" + (objName2 != null ? objName2.Name : "") + "</script>";
... where you put a += to concantinate the new part with the last. Note also that "" is effectively the same as String.Empty. You might benifit greatly from using "" instead of String.Empty, until you learn to make the code shorter and more readable.
For your code:
string str;
str = "<body lang=EN-US style='tab-interval:.5in'>";
str += "<div class=Section1>";
str += "<img img width=100 height=50 id='myImg' src='C:/test.jpg'>";
str += "<p style='font-weight:bold; color:green;'>";
str += "<u> Test Name </u></p>" + objTest.name.ToString() + "<br/>";
str += objname.name.ToString() != null ? string.Join(", ", objname.name.ToArray()) : String.Empty;
str += "</body> </html> ";
... is going to make this much easier to figure out. You shoudl try the StringBuilder aproach which is nearly the same as this. Should make it much easier overall, I promise :)
So specfically here are a few problems:
`... + objTest.name.ToString() + ...`
This will fail if objTest is null, and makes the
`objname.name.ToString() != null`
not mean much.
You probably intend to do something like this (for both your objTest and objname):
str += "<u> Test Name </u></p>" + (objname != null && objname.name != null ? objname.name.toString() : "") + "<br/>";
This protects both objname and objname.name.
Even better would be:
string str;
var objNameName = (objname != null) ? objname.name : null;
var objTestName = (objTest != null) ? objTest.name : null;
str = "<body lang=EN-US style='tab-interval:.5in'>";
str += "<div class=Section1>";
str += "<img img width=100 height=50 id='myImg' src='C:/test.jpg'>";
str += "<p style='font-weight:bold; color:green;'>";
str += "<u> Test Name </u></p>" + (objTestName != null ? objTestName.ToString() : "") + "<br/>";
str += objNameName != null ? string.Join(", ", objNameName.ToArray()) : "";
str += "</body> </html> ";