I've a User
class like this:
public class User
{
public string Name { get; set; }
public string Family { get; set; }
public string Type { get; set; }
}
And Program.cs
file:
public static void Main()
{
try
{
List<User> users = new List<User>();
users.Add(new User
{
Family = "Zamani",
Name = "Farhad",
Type = "ADY"
});
DoSomething(users);
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
}
public static void DoSomething(List<User> users)
{
var adult = users.Where(a => // line 36
a.Name.ToUpperInvariant() == "Farhad".ToUpperInvariant()
&& a.Family.ToUpperInvariant() == "zam".ToUpperInvariant()
).FirstOrDefault();
(string name, string type) = GetPassengerInfo(GetType(adult.Type), "farhad");//line 41
}
private static (string name, string type) GetPassengerInfo(string v, string d)
{
return (v, d);
}
static string GetType(string type)
{
string result = string.Empty;
switch (type)
{
case "ADT":
result = "Adult";
break;
}
return result;
}
When I run the program in debug mode, Stacktrace
of exception display Line:41
and it is ok.
But when I run the project by Release mode I get different line of code in Stacktrace
.
In Release mode it show Line:36
.
Why?
Why?
Because in release mode, the optimizer can inline functions and do other things that change the actual line number from what you have in source code.
Just outputting the stack trace is probably not helpful (IMHO). What type of exception did you get? What was the error message? What method did it originate from? Those are more helpful for diagnosing problems in my experience. I would change you exception handling to at least Console.WriteLine(ex)
. That will give you all of that data plus the stack trace.
This link can be helpful.