I'm seeing a different behavior than I expect of List.First()
:
I build some test app which contains a list
public abstract class GridTemplateModelBase : INotifyPropertyChanged, IDisposable
{
Public ObservableCollection<ColumnModel> Columns
{
get
{
return _columns;
}
protected set
{
_columns = value;
OnPropertyChanged();
}
}
}
The ColumnModel
class contains a property named GroupIndex
. I would like to choose particular item from Columns
list and set the GroupIndex
property through another class so I used the following code:
//choose the list item I want to change
var test = GridProfile.Columns.First(p => p.FieldName == item);
//Change GroupIndex value
test.GroupIndex = 1;
I would expect this code to change the GroupIndex
value of the list item but it seems that the `Columns.First()' method return a copy of this list item.
See the following images:
Initial GroupIndex of selected list item is -1
test parameter value from code above after GroupIndex value changing, as you can see it changed to 1
Value of selected list item after run the code above => still -1
If the List.First()
method returns a reference of selected list item, we had to see identical values for that list item and the test
parameter from the code above but surprisingly this is not the case.
Does anyone can explain how exactly the Linq.First()
method works?
Hoping my question is clear
Here is the ColumnModel
class content:
public class ColumnModel : INotifyPropertyChanged
{
/// <summary>
/// Initializes a new instance of the ColumnModel class.
/// </summary>
public ColumnModel()
{
AllowEdit = DefaultBoolean.True;
SortIndex = -1;
GroupIndex = -1;
}
private string _cellToolTip;
private int _sortIndex;
private ColumnSortOrder _sortOrdering;
private bool _isReadOnly;
private int _groupIndex;
private bool _isVisible;
private bool _fixedWidth;
private double _minWidth;
private double _width;
private EditSettingsHorizontalAlignment _cellContentAlighnment;
private ColumnProfile _profileTemplate;
private string _fieldHeader;
private string _fieldName;
private DefaultBoolean allowEdit;
// Specifies the name of a data source field to which the column is bound.
public string FieldName
{
get
{
return _fieldName;
}
set
{
_fieldName = value;
OnPropertyChanged();
}
}
public string FieldHeader
{
get
{
return _fieldHeader;
}
set
{
_fieldHeader = value;
OnPropertyChanged();
}
}
// Specifies the type of template to load
public ColumnProfile ProfileTemplate
{
get
{
return _profileTemplate;
}
set
{
_profileTemplate = value;
OnPropertyChanged();
}
}
public EditSettingsHorizontalAlignment CellContentAlighnment
{
get
{
return _cellContentAlighnment;
}
set
{
_cellContentAlighnment = value;
OnPropertyChanged();
}
}
public double Width
{
get
{
return _width;
}
set
{
_width = value;
OnPropertyChanged();
}
}
public double MinWidth
{
get
{
return _minWidth;
}
set
{
_minWidth = value;
OnPropertyChanged();
}
}
public bool FixedWidth
{
get
{
return _fixedWidth;
}
set
{
_fixedWidth = value;
OnPropertyChanged();
}
}
public bool IsVisible
{
get
{
return _isVisible;
}
set
{
_isVisible = value;
OnPropertyChanged();
}
}
public int GroupIndex
{
get
{
return _groupIndex;
}
set
{
_groupIndex = value;
OnPropertyChanged();
}
}
public bool IsReadOnly
{
get
{
return _isReadOnly;
}
set
{
_isReadOnly = value;
OnPropertyChanged();
}
}
public ColumnSortOrder SortOrdering
{
get
{
return _sortOrdering;
}
set
{
_sortOrdering = value;
OnPropertyChanged();
}
}
public DefaultBoolean AllowEdit
{
get { return allowEdit; }
set
{
allowEdit = value;
OnPropertyChanged();
}
}
public string CellToolTip
{
get
{
return _cellToolTip;
}
set
{
_cellToolTip = value;
OnPropertyChanged();
}
}
public int SortIndex
{
get
{
return _sortIndex;
}
set
{
_sortIndex = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
Update:
By your comments I'm understanding that List.First()
returns reference type, probably my code contains an issue or my debugger might returns wrong result - will check it.
Here is First
source code (credit: @Enigmativity):
public static TSource First<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
if (source == null)
throw Error.ArgumentNull("source");
if (predicate == null)
throw Error.ArgumentNull("predicate");
foreach (TSource source1 in source)
{
if (predicate(source1))
return source1;
}
throw Error.NoMatch();
}
Thank you