.net: debug highlight string not actually running

Copy and paste the following into a new console application in VS. Add links to System.Web and System.Web.Services (I know that console applications do not need these assemblies, I will just show you a piece of code that does not work in my web application).

Although both conditions in the if expression are false, it turns out to be true. Does anyone know the reason? (Visual Studio 2008 9.0.30729.1) .NET 3.5 SP1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Services;
using System.Web;

namespace ConsoleApplication8
{
    class Program
    {
        static void Main(string[] args)
        {
            string x = "qweqweqw";
            string y =
                "{\"textMedia\":[-1,-1,-1,-1,-1],\"textOperand\":[1,1,1,1,1],\"textString\":[\"\",\"\",\"\",\"\",\"\"],\"dateSite\":[-11],\"dateOperand\":[],\"dateString\":[],\"status\":[-11,0,0],\"media\":[-11,0,0],\"subItem\":true,\"context\":false,\"branchSearch\":false,\"profileIDs\":[2,5,18],\"profileViewIDs\":[48,58,38],\"currentSelectedBranch\":0}";

            SaveSearch(x, y);
        }

        [WebMethod]
        public static object SaveSearch(string name, string encodedSearch)
        {
            object response = new { };

            string x = name;
            string y = encodedSearch;

            // Why does this if statement throw an exception if both equal false?
            if (x.Trim().Equals(string.Empty) || y.Trim().Equals(string.Empty))
                throw new AjaxErrorException("Save Search", "Something went wrong", "JSFunction");


            try
            {
                {
                    return new
                    {
                        error = false,
                        name = name,
                        userID = 123,
                        date = DateTime.Now
                    };
                }
            }
            catch (Exception ex)
            {
                String e;

                if (HttpContext.Current.IsDebuggingEnabled)
                    e = ex.Message + "\n\n" + ex.StackTrace;
                else
                    e = "error error aliens approaching";

                throw new AjaxErrorException("Save Search", e, "");
            }

            return response;
        }

        public class AjaxErrorException : System.Exception
        {
            public AjaxErrorException(string title, string details, string function)
                : base(title)
            { }
            string _stackTrace;
            public override string StackTrace
            {
                get
                {
                    return _stackTrace;
                }
            }
        }
    }
}
+5
source share
6 answers

, , if (throw), . , IDE, IL Debugger, , throw. , . , Visual Studio 2008

assert if, , , .

System.Diagnostics.Debug.Assert(false);
+8

vs2008 . :

, , . , 30 ( if), ( ) . - . 31 ( ), .

, . -, . , , , , .

+1

null - , Empty. null.Equals(string.Empty) false. null.Trim() , null . string.IsNullOrEmpty(), @Otavio, . , , , .

, , .Net 4 , , .


, :

if (string.IsNullOrEmpty(x) || string.IsNullOrEmtpy(y) || x.Trim().Length == 0 || y.Trim().Length == 0)
0

:

    string x = "   text   \t\t\n";
    string y = "not empty";

    if (x.Trim().Equals(string.Empty) || y.Trim().Equals(string.Empty))
        Console.WriteLine("TRUE");

'TRUE', . - , , "if"?

: throw . , , ?

0

. -, obj bin . .

, Visual Studio.

0

/ , , , .

, , ? , NullReferenceException.

In any case, maybe my answer was not clear enough :(

-1
source

All Articles