System.Net.Http.Headers.WarningHeaderValue.Equals C# (CSharp) Method

Equals() public method

public Equals ( object obj ) : bool
obj object
return bool
        public override bool Equals(object obj)
        {
            WarningHeaderValue other = obj as WarningHeaderValue;

            if (other == null)
            {
                return false;
            }

            // 'agent' is a host/token, i.e. use case-insensitive comparison. Use case-sensitive comparison for 'text'
            // since it is a quoted string.
            if ((_code != other._code) || (!string.Equals(_agent, other._agent, StringComparison.OrdinalIgnoreCase)) ||
                (!string.Equals(_text, other._text, StringComparison.Ordinal)))
            {
                return false;
            }

            // We have a date set. Verify 'other' has also a date that matches our value.
            if (_date.HasValue)
            {
                return other._date.HasValue && (_date.Value == other._date.Value);
            }

            // We don't have a date. If 'other' has a date, we're not equal.
            return !other._date.HasValue;
        }

Usage Example

        public void Equals_UseSameAndDifferentRanges_EqualOrNotEqualNoExceptions()
        {
            WarningHeaderValue warning1 = new WarningHeaderValue(214, "host", "\"Transformation applied\"");
            WarningHeaderValue warning2 = new WarningHeaderValue(214, "HOST", "\"Transformation applied\"");
            WarningHeaderValue warning3 = new WarningHeaderValue(215, "host", "\"Transformation applied\"");
            WarningHeaderValue warning4 = new WarningHeaderValue(214, "other", "\"Transformation applied\"");
            WarningHeaderValue warning5 = new WarningHeaderValue(214, "host", "\"TRANSFORMATION APPLIED\"");
            WarningHeaderValue warning6 = new WarningHeaderValue(214, "host", "\"Transformation applied\"",
                new DateTimeOffset(2010, 7, 19, 18, 31, 27, TimeSpan.Zero));
            WarningHeaderValue warning7 = new WarningHeaderValue(214, "host", "\"Transformation applied\"",
                new DateTimeOffset(2011, 7, 19, 18, 31, 27, TimeSpan.Zero));
            WarningHeaderValue warning8 = new WarningHeaderValue(214, "host", "\"Transformation applied\"",
                new DateTimeOffset(2010, 7, 19, 18, 31, 27, TimeSpan.Zero));

            Assert.False(warning1.Equals(null), "214 host \"t.a.\" vs. <null>");
            Assert.True(warning1.Equals(warning2), "214 host \"t.a.\" vs. 214 HOST \"t.a.\"");
            Assert.False(warning1.Equals(warning3), "214 host \"t.a.\" vs. 215 host \"t.a.\"");
            Assert.False(warning1.Equals(warning4), "214 host \"t.a.\" vs. 214 other \"t.a.\"");
            Assert.False(warning1.Equals(warning6), "214 host \"t.a.\" vs. 214 host \"T.A.\"");
            Assert.False(warning1.Equals(warning7), "214 host \"t.a.\" vs. 214 host \"t.a.\" \"D\"");
            Assert.False(warning7.Equals(warning1), "214 host \"t.a.\" \"D\" vs. 214 host \"t.a.\"");
            Assert.False(warning6.Equals(warning7), "214 host \"t.a.\" \"D\" vs. 214 host \"t.a.\" \"other D\"");
            Assert.True(warning6.Equals(warning8), "214 host \"t.a.\" \"D\" vs. 214 host \"t.a.\" \"D\"");
        }