访问没有对象名称和属性名称的属性? [英] accessing properties without object name and property name ?

查看:93
本文介绍了访问没有对象名称和属性名称的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常如果我需要Accces类属性,比我用来编写objectname.properties名称(tempRecord),但是怎么来这里工作没有写objectname.properties名称



  class  TempRecord 
{

private float [] temps = new float [ 10 ] { 56 .2F, 56 .7F, 56 .5F, 56 .9F, 58 .8F,
61 .3F, 65 .9F , 62 .1F, 59 .2F, 57 .5F};
private int vps;

public float this [ int index]
{
get
{
return temps [index];
}

set
{
temps [index] = ;
}
}

public int mava
{
set
{
vps = value ;
}
获取
{
返回 vps + VPS;
}

}
}

class MainClass
{
static void Main()
{
TempRecord tempRecord = new TempRecord();
tempRecord.mava = 4 ;
// 通常如果我需要Accces类属性,比我用来写objectname.properties名称(tempRecord) ,
// 但是如何在这里工作没有编写objectname.properties名称
tempRecord [ 3 ] = 58 .3F; // 这里没有编写objectname.properties名称
tempRecord [ 5 ] = 60 .1F; // 此处无编写objectname.properties名称
for int i = 0 ; i < 10 ; i ++)
{
System.Console.WriteLine( 元素#{0} = {1},i,tempRecord [i]);
}
System.Console.WriteLine( 按任意键退出。);
System.Console.ReadKey();

}
}

解决方案

你刚刚发现了C#Indexers,一个句法方便,使您能够创建一个类,结构或接口,客户端应用程序可以像数组一样访问[ ^ ]。



您定义的不是'属性,它相当于:

  public   class  TempRecord:List< float> 
{
// 无论
}

索引器可以做一些有趣的事情[ ^ ]但是,imho,我不想使用它们,因为我认为许多人不熟悉这种C#语法会使代码不那么自我记录。



如果我希望一个类行为像(它体现:)一个List,我更喜欢让类继承自List。


我很确定这是一个结果'this'允许你引用类并访问内部数组



  public   float     [ int  index] 
{
get
{
return temps [index];
}

set
{
temps [index] = ;
}
}





请注意,我很有趣,因为我可以看到会发生什么以及它是如何工作的,我以前从未真正看过它使用



- CP显示代码标签但正确处理格式 - 奇怪 [/ edit ]


Usually If I Need Accces Class properties , Than I Used To Write objectname.properties name(tempRecord) , But How Come Here It Work's Without Writing objectname.properties name

class TempRecord
{
    
    private float[] temps = new float[10] { 56.2F, 56.7F, 56.5F, 56.9F, 58.8F, 
                                            61.3F, 65.9F, 62.1F, 59.2F, 57.5F };
    private int vps;
    
    public float this[int index]
    {
        get
        {
            return temps[index];
        }

        set
        {
            temps[index] = value;
        }
    }

    public int mava
    {
        set
        {
            vps = value;
        }
        get
        {
            return vps + vps;
        }

    }
}

class MainClass
{
    static void Main()
    {
        TempRecord tempRecord = new TempRecord();
        tempRecord.mava = 4;
        // Usually If I Need Accces Class properties , Than I Used To Write objectname.properties name(tempRecord) , 
        //But How Come Here It Work's Without Writing objectname.properties name 
        tempRecord[3] = 58.3F; // Here Without Writing objectname.properties name
        tempRecord[5] = 60.1F; // here Without Writing objectname.properties name
        for (int i = 0; i < 10; i++)
        {
            System.Console.WriteLine("Element #{0} = {1}", i, tempRecord[i]);
        }
        System.Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();

    }
}

解决方案

You have just "discovered" C# Indexers, "a syntactic convenience that enable you to create a class, struct, or interface that client applications can access just as an array" [^].

What you defined is not a 'Property, and it's equivalent to:

public class TempRecord : List<float>
{
   // whatever
}

There are some interesting things you could do with Indexers [^], but, imho, I prefer not to use them because I think the fact that many people are unfamiliar with this C# syntax makes the code less self-documenting.

If I want a Class to "behave" like (it "embodies:) a List, I prefer to have the Class inherit from List.


I'm pretty sure this is a result of the 'this' here that allows you to refer to the class and access the internal array as you do

public float this[int index]
    {
        get
        {
            return temps[index];
        }
 
        set
        {
            temps[index] = value;
        }
    }



mind you, its intriguing me because I can see what happens and how it works, Ive never actually seen it used before

[edit] - CP is displaying the code tags but handling the formatting correctly - bizarre [/edit]


这篇关于访问没有对象名称和属性名称的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆