zoukankan      html  css  js  c++  java
  • C# 属性和字段

    不将理论,直接看不同的代码产生什么样的影响

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

     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;
            }
        }
    }
  • 相关阅读:
    LeetCode 38. 外观数列
    LeetCode 33. 搜索旋转排序数组
    LeetCode 31. 下一个排列
    LeetCode 34. 在排序数组中查找元素的第一个和最后一个位置
    LeetCode 29. 两数相除
    LeetCode 22. 括号生成
    LeetCode 1. 两数之和
    LeetCode 17. 电话号码的字母组合
    LeetCode 18. 四数之和
    LeetCode 16. 最接近的三数之和
  • 原文地址:https://www.cnblogs.com/mrxiaohe/p/5856901.html
Copyright © 2011-2022 走看看