Search Results

Search found 3 results on 1 pages for 'comecme'.

Page 1/1 | 1 

  • How to determine if two generic type values are equal?

    - by comecme
    I'm trying to figure out how I can successfully determine if two generic type values are equal to each other. Based on Mark Byers' answer on this question I would think I can just use value.Equals() where value is a generic type. My actual problem is in a LinkedList implementation, but the problem can be shown with this simpler example. class GenericOjbect<T> { public T Value { get; private set; } public GenericOjbect(T value) { Value = value; } public bool Equals(T value) { return (Value.Equals(value)); } } Now I define an instance of GenericObject<StringBuilder> containing new StringBuilder("StackOverflow"). I would expect to get true if I call Equals(new StringBuilder("StackOverflow") on this GenericObject instance, but I get false. A sample program showing this: using System; using System.Text; class Program { static void Main() { var sb1 = new StringBuilder("StackOverflow"); var sb2 = new StringBuilder("StackOverflow"); Console.WriteLine("StringBuilder compare"); Console.WriteLine("1. == " + (sb1 == sb2)); Console.WriteLine("2. Object.Equals " + (Object.Equals(sb1, sb2))); Console.WriteLine("3. this.Equals " + (sb1.Equals(sb2))); var go1 = new GenericOjbect<StringBuilder>(sb1); var go2 = new GenericOjbect<StringBuilder>(sb2); Console.WriteLine("\nGenericObject compare"); Console.WriteLine("1. == " + (go1 == go2)); Console.WriteLine("2. Object.Equals " + (Object.Equals(go1, go2))); Console.WriteLine("3. this.Equals " + (go1.Equals(go2))); Console.WriteLine("4. Value.Equals " + (go1.Value.Equals(go2.Value))); } } For the three methods of comparing two StringBuilder objects, only the StringBuilder.Equals instance method (the third line) returns true. This is what I expected. But when comparing the GenericObject objects, its Equals() method (the third line) returns false. Interestingly enough, the fourth compare method does return true. I'd think the third and fourth comparison are actually doing the same thing. I would have expected true. Because in the Equals() method of the GenericObject class, both value and Value are of type T which in this case is a StringBuilder. Based on Mark Byers' answer in this question, I would've expected the Value.Equals() method to be using the StringBuilder's Equals() method. And as I've shown, the StringBuilder's Equal() method does return true. I've even tried public bool Equals(T value) { return EqualityComparer<T>.Default.Equals(Value, value); } but that also returns false. So, two questions here: Why doesn't the code return true? How could I implement the Equals method so it does return true?

    Read the article

  • Virtual member call in a constructor when assigning value to property

    - by comecme
    I have an Abstract class and a Derived class. The abstract class defines an abstract property named Message. In the derived class, the property is implemented by overriding the abstract property. The constructor of the derived class takes a string argument and assigns it to its Message property. In Resharper, this assignment leads to a warning "Virtual member call in constructor". The AbstractClass has this definition: public abstract class AbstractClass { public abstract string Message { get; set; } protected AbstractClass() { } public abstract void PrintMessage(); } And the DerivedClass is as follows: using System; public class DerivedClass : AbstractClass { private string _message; public override string Message { get { return _message; } set { _message = value; } } public DerivedClass(string message) { Message = message; // Warning: Virtual member call in a constructor } public DerivedClass() : this("Default DerivedClass message") {} public override void PrintMessage() { Console.WriteLine("DerivedClass PrintMessage(): " + Message); } } I did find some other questions about this warning, but in those situations there is an actual call to a method. For instance, in this question, the answer by Matt Howels contains some sample code. I'll repeat it here for easy reference. class Parent { public Parent() { DoSomething(); } protected virtual void DoSomething() {}; } class Child : Parent { private string foo; public Child() { foo = "HELLO"; } protected override void DoSomething() { Console.WriteLine(foo.ToLower()); } } Matt doesn't describe on what error the warning would appear, but I'm assuming it will be on the call to DoSomething in the Parent constructor. In this example, I understand what is meant by a virtual member being called. The member call occurs in the base class, in which only a virtual method exists. In my situation however, I don't see why assigning a value to Message would be calling a virtual member. Both the call to and the implementation of the Message property are defined in the derived class. Although I can get rid of the error by making my Derived Class sealed, I would like to understand why this situation is resulting in the warning.

    Read the article

  • Overloading Console.ReadLine possible? (or any static class method)

    - by comecme
    I'm trying to create an overload of the System.Console.ReadLine() method that will take a string argument. My intention basically is to be able to write string s = Console.ReadLine("Please enter a number: "); in stead of Console.Write("Please enter a number: "); string s = Console.ReadLine(); I don't think it is possible to overload Console.ReadLine itself, so I tried implementing an inherited class, like this: public static class MyConsole : System.Console { public static string ReadLine(string s) { Write(s); return ReadLine(); } } That doesn't work though, cause it is not possible to inherit from System.Console (because it is a static class which automatically makes is a sealed class). Does it make sense what I'm trying to do here? Or is it never a good idea to want to overload something from a static class?

    Read the article

1