What is the main reason for this comparison? (amazing result for me)

Context: I am prototyping to prepare (possibly) converting my WinForms application to WPF.

I am making a very simple tree view event handler for which this code:

var treeViewItem = (TreeViewItem)e.NewValue; var treeViewItemTag = treeViewItem.Tag; if (treeViewItemTag == "ViewForAMs") { ObjectQuery<AccountManagerView> oq = entities.AccountManagerViews; var q = from c in oq select c; dataGrid1.ItemsSource = q.ToList(); } 

and xaml:

 <Window x:Class="AccountingWpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded"> <DockPanel> <TreeView Name="treeView1" ItemsSource="{Binding Folders}" SelectedItemChanged="treeView1_SelectedItemChanged"> <TreeViewItem Header="Account Manager View" Tag="ViewForAMs"/> </TreeView> <DataGrid AutoGenerateColumns="True" Name="dataGrid1" /> </DockPanel> </Window> 

When I launched it, I completely expected to see that my data grid is full, but the comparison == happened in the second line of code above.

The debugger shows this:

QUESTION: why there were no compilation errors or runtime? (the same question is different: what is actually being compared, so does the == operator output FALSE?)

enter image description here

+7
source share
7 answers

Transfer Tag to string . In the default implementation, object , == compares links. Because the Tag property is of type object , it uses the lowest common == operator between object and string , which is an implementation of object . When Tag clicked on string , the implementation on string , which is a comparison of values.

+4
source

Use Object.Equals(treeViewItemTag, "ViewForAMs") instead

+4
source

If you look at the treeViewItemTag type, you will see that the type is an object, not a string. Therefore, when you use ==, you are comparing the links of two objects. This will always return false in this case. If you use Equals () instead or pass a string, then it should work.

+2
source

Use Equals () to compare strings.

UPDATE: Or translate both values ​​into strings. Example from MSDN:

 string a = "hello"; string b = "h"; // Append to contents of 'b' b += "ello"; Console.WriteLine(a == b); Console.WriteLine((object)a == (object)b); 

The first comparison returns true, but the second returns false (because it compares links, not strings).

+1
source

The tag in the TreeViewItem property is an object, not a string. == compares object references. To compare a string, you must do a comparison using ToString ():

 if (treeViewItemTag.ToString() == "ViewForAMs") 

But you have to be sure what the string contains, otherwise the comparison will also fail.

+1
source

'treeViewItem.Tag' - a link to an object. By default in C #, the == operator checks reference equality, i.e. That both objects are the same in memory. The string has an operator '==', overloaded to check for equality of values, i.e. If the lines contain the same content. However, to use it, you need to specify "treeViewItem.Tag" in the string.

0
source

I do not own WPF, but in the context of Winforms, I would say that Tag is of type Object.
The equality operator on the object compares the links.

If you (or some other reader) can find out why this still works in some cases:
When you built strings using StringBuilder or unmanaged functions, you will not get the string so.called intern. This means that there is a way that you have two different string objects at runtime that have the same content.
Typically, strings refer to the same instance, unless they are created at run time, as described above. You can call String.Intern to get the old link to the string with the same content. They must be the same for the same content. There are situations where knowing this tiny detail can be a big help or discovery.

0
source

All Articles