如何创建自定义属性,如Length属性? [英] How to create custom property like Length property?
本文介绍了如何创建自定义属性,如Length属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
class Program
{
static void Main(string[] args)
{
string str = "Hello";
for (int i = 0; i < str.Length; i++)
{
Console.WriteLine(str[i]);
}
}
}
在上面的程序中,我使用了Length属性,我不想使用字符串Length属性,我想创建类似Length的属性.逻辑是什么,如何实现?
In the above program, I have used Length property, I dont want to use string Length property, i want to create a similar property like Length. What is the logic and how can i achieve it?
推荐答案
属性是非常基本的东西,您可以在任何C#教程中进行阅读.在网络上或任何有关C#的入门书籍中.这是有关C#的(合法的)免费PDF书籍:使用C#编程简介 [
Properties are very basic stuff and you''ll be able to read up on that in any C#-tutorial on the web or in any introductory book on C#. Here''s a (legal) free PDF book on C#: Introduction to Programming with C#[^]
However, I''ll add that it''ll depend on whether you want to add a property to a class that you wrote yourself or to a class from the .Net Framework. The latter case is not possible because there are no true extension properties in C# yet. Only workarounds that achieve somewhat the same goal but don''t work the same way. You''ll find them by googling for "C# extension properties".
这篇关于如何创建自定义属性,如Length属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文