您的位置:首页 > 编程语言 > C#

C# 属性和字段

2016-09-09 15:43 295 查看
不将理论,直接看不同的代码产生什么样的影响

下面代码中有字段和其对应的属性:

private string title;

public string Title ...

通过做实验可知,当我给属性赋值,即Title="News";此时如果单步调试,会进入到属性的set代码段中,而且如果我们一直关注title和Title的值,可以发现,只有当title = value;这句话成功执行之后,title和Title才会一起变成新的值。也就是说只有当字段title被成功赋值,属性Title才会跟被赋值。如果我们执行代码段2,由于if(true),使得title = value;没有得到执行,我们可以看到,最终的结果是title和Title都没有变,还是原来的老值。或者不执行代码段2,当老值和新值相同的时候,也能忽略掉title=value;

代码段1:

private string title;

public string Title
{
get { return title; }
//set { Set(ref title, value); }
set
{
if (title == value) { return; }
title = value;
RaisePropertyChanged(() => Title);
}
}


代码段2

private string title;

public string Title
{
get { return title; }
//set { Set(ref title, value); }
set
{
if (title == value) { return; }
if (true)
{
return;
}
title = value;
RaisePropertyChanged(() => Title);
}
}


如果我们如果给字段(private)赋值,即title="news",此时是不会进入到属性Title的set代码段的。而且执行完成之后,属性Title和字段title,会同时被赋予新值"news"。

前台绑定的是Title(因为Title是public,而title是private),所以如果要让前台做出变化,需要在赋值语句 title="news";后面再执行 RaisePropertyChanged(() => Title);

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.CommandWpf;
using System.Text.RegularExpressions;
using System.Windows.Input;

namespace MVVM.ViewModel
{
public class MainViewModel : ViewModelBase
{
private string title;

public string Title
{
get { return title; }
//set { Set(ref title, value); }
set
{
if (title == value) { return; }
title = value;
RaisePropertyChanged(() => Title);
}
}

public ICommand ChangeTitleCommand { get; set; }

public MainViewModel()
{
Title = "Hello World";
ChangeTitleCommand = new RelayCommand(ChangeTitle);
}
private void ChangeTitle()
{
string res="";
string line = "root@HZ-CAS01-CVK01:~#";
string pattern = ".*@(.*):.*";
Regex re = new Regex(pattern);
MatchCollection matches = re.Matches(line);
foreach (Match m in matches)
{
GroupCollection groups = m.Groups;
res = groups[1].Value;
}
//在这里直接对字段赋值,不进入set代码段,属性和字段同时被赋予新值news
title = "news";
RaisePropertyChanged(() => Title);

}
}
}


using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.CommandWpf;
using System.Text.RegularExpressions;
using System.Windows.Input;

namespace MVVM.ViewModel
{
/// <summary>
/// This class contains properties that the main View can data bind to.
/// <para>
/// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
/// </para>
/// <para>
/// You can also use Blend to data bind with the tool's support.
/// </para>
/// <para>
/// See http://www.galasoft.ch/mvvm /// </para>
/// </summary>
public class MainViewModel : ViewModelBase
{
private string title;

public string Title
{
get { return title; }
set
{
if (title == value) { return; }
title = value;
RaisePropertyChanged(() => Title);
}
}

public ICommand ChangeTitleCommand { get; set; }
/// <summary>
/// Initializes a new instance of the MainViewModel class.
/// </summary>
public MainViewModel()
{
Title = "Hello World";
ChangeTitleCommand = new RelayCommand(ChangeTitle);

////if (IsInDesignMode)
////{
////    // Code runs in Blend --> create design time data.
////}
////else
////{
////    // Code runs "for real"-
////}
}
private void ChangeTitle()
{
string res="";
string line = "root@HZ-CAS01-CVK01:~#";
string line1 = "<WX5504>";
string line2 = "[fangkuohao]";
//string pattern = "@(?:.*@(.*):~#)|(?:<(.*)>)|(?<=[)(.*)(?=>])|(?:(.*)>|#))";
string pattern = ".*@(.*):.*";
Regex re = new Regex(pattern);
MatchCollection matches = re.Matches(line);
foreach (Match m in matches)
{
GroupCollection groups = m.Groups;
res = groups[1].Value;
}
Title = res;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: