0

I wrote a method-extension for to simualte List's First() but the returned value (actually reference, isn't?) doesn't work if I try to change a property of it, for example;

HtmlElement ele = webBrowser1.Document.All.GetElementsByName("foo").First();
ele.InnerText = "hello!"; // doesn't Works. That value isn't changed. Why?
webBrowser1.Document.All.GetElementsByName("foo")[0].InnerText = "abc"; // but this does Works

Here's First() function:

   public static HtmlElement First(this HtmlElementCollection a)
        {
            if (a == null)
                throw new ArgumentNullException();
            if (a.Count == 0)
                throw new InvalidOperationException();

            return a[0];
        }

Why using return from a.First().value = foo doesn't Works but arr[0].value = "hehe"; does? how do I fix it? do I need to learn how to use returns ref?

Jack
  • 16,276
  • 55
  • 159
  • 284
  • you can test if the result from First() and [0] is the same reference, they should be same, HtmlElement is a Class, so the instance of it is on the heap, doesn't make sence they don't equal – Sean Nov 27 '14 at 02:22

1 Answers1

1

based on this post: LINQ: Select an object and change some properties without creating a new object

var ele = webBrowser1.Document.All.GetElementsByName("foo")
                              .First().Select(e => { e.InnerText = "hello"; return e; });
Community
  • 1
  • 1
ymz
  • 6,602
  • 1
  • 20
  • 39