属性(Property)是类(class)、结构(structure)和接口(interface)的命名(named)成员。类或结构中的成员变量或方法称为 域(Field)。属性(Property)是域(Field)的扩展,且可使用相同的语法来访问。它们使用 访问器(accessors) 让私有域的值可被读写或操作。

该代码主要是帮助读者了解属性的用法,代码实现了添加属性值和根据添加的属性值进行筛选

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text; namespace 编码练习
{
//创建一个类包含两个属性
class People
{
public string Name { get; set; }
public string Address { get; set; }
public People(string name, string address)
{
this.Name = name;
this.Address = address;
} }
//继承两个接口(枚举/格式化)
class Peoples : IEnumerable
{
//创建一个对象列表
private List<People> Lpeoples { get; set; }
private StringBuilder Sbuilder { get; set; }
public Peoples()
{
Lpeoples = new List<People>();
}
//创建一个可以往列表加属性实例的方法
public void Add(People people)
{
Lpeoples.Add(people);
}
//获取对象值
public IEnumerator GetEnumerator()
{
foreach (var p in Lpeoples)
{
yield return p;
}
} public override string ToString()
{
return GetContent(Lpeoples);
}
public string Tostring(string format)
{
return ToString(format, CultureInfo.CreateSpecificCulture("zh-CN"));
}
public string ToString(string format, IFormatProvider formatProvider)
{
IEnumerable<People> ps = Lpeoples;
if (format.ToUpperInvariant() == "B")
{
ps = from p in Lpeoples where p.Address == "北京" select p;
}
else if (format.ToUpperInvariant() == "S")
{
ps = from p in Lpeoples where p.Address == "上海" select p;
}
return GetContent(ps);
}
//将数据连接到数组
private string GetContent(IEnumerable<People> peoples)
{
Sbuilder = new StringBuilder();
foreach (var p in peoples)
{
Sbuilder.AppendLine(string.Format("{0}{1}", p.Name, p.Address));
}
return Sbuilder.ToString();
}
}
public class Start
{
public static void Main()
{
Peoples peoples = new Peoples()
{new People("zhangsan","北京"),new People("lisi","上海"),new People("wangwu","北京"),new People("naliu","北京")};
Console.WriteLine("本站会员有:");
Console.WriteLine(peoples.ToString());
Console.WriteLine("北京的会员有:");
Console.WriteLine(peoples.Tostring("B"));
Console.WriteLine("上海的会员有:");
Console.WriteLine(peoples.Tostring("S"));
Console.ReadLine(); }
}
}

附加知识:

  IFormatProvider接口的引用,该接口用于格式化操作,当进行数字,日期时间,字符串匹配时,都会进行CultureInfo的操作,文中CultureInfo.CreateSpecificCulture("zh-CN")意为结果输出的是<简体,中国>

c#属性(Property)的更多相关文章

  1. 区分元素特性attribute和对象属性property

    × 目录 [1]定义 [2]共有 [3]例外[4]特殊[5]自定义[6]混淆[7]总结 前面的话 其实attribute和property两个单词,翻译出来都是属性,但是<javascript高 ...

  2. 属性(@property)、@synthesize

    先前我们学的实例变量是这样的 { int _age; int _height; int age; } 后来学属性 @property int age; 看到@property 会自动编译生成某个成员变 ...

  3. Object的属性property详细解释(自动生成成员变量)

    类Class中的属性property: 在ios第一版中,我们为输出口同时声明了属性和底层实例变量,那时,属性是oc语言的一个新的机制,并且要求你必须声明与之对应的实例变量,例如: @interfac ...

  4. OC 实例变量(instance var)与属性(@property)的关系 isa指针

    实例变量(instance var)与属性(@property)的关系 Objective-C 2.0之后,声明一个@property name自动产生一个实例变量,名为_name,因此省去实例变量和 ...

  5. iOS中属性Property的常用关键字的使用说明

    属性关键字的作用 现在我们iOS开发中,基本都是使用ARC(自动引用计数)技术,来编写我们的代码.因此在属性property中我们经常使用的关键字有strong,weak,assign,copy,no ...

  6. Effective C# 学习笔记(原则一:始终能的使用属性(property),而不是可直接访问的Data Member)

    原则一:始终能的使用属性(property),而不是可直接访问的Data Member    Always use properties instead of accessible data memb ...

  7. 对用户控件(ascx)属性(property)赋值

    对用户控件(ascx)属性(property)赋值 Insus.NET写此博文,是对用户控件(ASCX)的属性赋值经验与技巧分享.是这样子的,在做新闻站点时,一般都会有分很多类别. 在站点首页会显示最 ...

  8. Yii2基本概念之——属性(property)

    学习任何一门学问,往往都是从起基本的概念学起.万丈高楼平地起,这些基本概念就是高楼的基石,必须做详尽的分析.我们知道,Yii2是一款脉络清晰的框架,理顺了基础的概念和基本功能,学习更高级和复杂的功能就 ...

  9. Day8 封装 静态属性property

    封装:将类的属性隐藏 #先看如何隐藏 1,在定义的属性之前加入__. class Foo: __N=111111 #_Foo__N def __init__(self,name): self.__Na ...

  10. 理解特性attribute 和 属性property的区别 及相关DOM操作总结

    查一下英语单词解释,两个都可以表示属性.但attribute倾向于解释为特质,而property倾向于解释私有的.这个property的私有解释可以更方便我们下面的理解. 第一部分:区别点 第一点:  ...

随机推荐

  1. Celery的使用完成异步任务与定时任务

    0917自我总结 Celery的使用 一.官方文档 Celery 官网:http://www.celeryproject.org/ Celery 官方文档英文版:http://docs.celeryp ...

  2. PHP 插入排序 -- 折半查找

    1. 折半查找  -- Binary Insertion Sort 时间复杂度 : O(n^2) 适用条件 : 相对直接插入排序,减少了数值的比较次数.适用于需要排序的数码比较少的情况. <?p ...

  3. LeetCode初级算法--树01:二叉树的最大深度

    LeetCode初级算法--树01:二叉树的最大深度 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.n ...

  4. 存储物理页属性的PFN数据库

    Windows内核分析索引目录:https://www.cnblogs.com/onetrainee/p/11675224.html 存储物理页属性的PFN数据库 一.PFN的基础概念 页帧:即CPU ...

  5. Spring Boot 2.X(十):自定义注册 Servlet、Filter、Listener

    前言 在 Spring Boot 中已经移除了 web.xml 文件,如果需要注册添加 Servlet.Filter.Listener 为 Spring Bean,在 Spring Boot 中有两种 ...

  6. python学习-函数和lambda表达式(五)

    5.2函数参数 位置参数:根据位置传入参数 关键字参数:根据参数名来传入参数 def girth(width, height): print("width:", width) pr ...

  7. oracle中创建用户、角色、权限简单使用

    Oracle关于用户.权限.角色简单使用 创建数据库用户(在system用户下)create user 用户名 identified by 密码; 授权grant 权限名 to 用户名; 查看当前用户 ...

  8. django-模板之自动转义autoescape(八)

    index.html {{QQ}} views.py def index(request): context={ 'QQ':'<a href="http://www.qq.com&qu ...

  9. Spring Boot2 系列教程(十九)Spring Boot 整合 JdbcTemplate

    在 Java 领域,数据持久化有几个常见的方案,有 Spring 自带的 JdbcTemplate .有 MyBatis,还有 JPA,在这些方案中,最简单的就是 Spring 自带的 JdbcTem ...

  10. 陈莉君教授: 回望踏入Linux内核之旅

    本文系转载,著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 作者: 陈莉君 来源: 微信公众号linux阅码场(id: linuxdev) 初次踏入Linux 几多耕耘,几多收获 ...