热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

将数组键设置为字符串而不是int?-Setarraykeyasstringnotint?

Iamtryingtosetthearraykeysasastringslikeintheexamplebelow,butinC#.我试图将数组键设置为字符串,如下

I am trying to set the array keys as a strings like in the example below, but inC#.

我试图将数组键设置为字符串,如下面的示例所示,但是是inC#。


6 个解决方案

#1


52  

The closest you get in C# is Dictionary:

最接近c#的是Dictionary :

var dict = new Dictionary();
dict["key_name"] = "value1";

Note that a Dictionary is not the same as PHP's associative array, because it is only accessible by one type of key (TKey -- which is string in the above example), as opposed to a combination of string/integer keys (thanks to Pavel for clarifying this point).

注意,Dictionary 与PHP的关联数组不同,因为它只能由一种类型的键(TKey——在上面的示例中是字符串)访问,而不是字符串/整数键的组合(感谢Pavel澄清了这一点)。

That said, I've never heard a .NET developer complain about that.

也就是说,我从来没有听到。net开发人员对此有过抱怨。


In response to your comment:

回应你的评论:

// The number of elements in headersSplit will be the number of ':' characters
// in line + 1.
string[] headersSplit = line.Split(':');

string hname = headersSplit[0];

// If you are getting an IndexOutOfRangeException here, it is because your
// headersSplit array has only one element. This tells me that line does not
// contain a ':' character.
string hvalue = headersSplit[1];

#2


5  

Uhm I'm guessing you want a dictionary:

嗯,我猜你想要一本字典:

using System.Collections.Generic;

// ...

var dict = new Dictionary();
dict["key_name1"] = "value1";
dict["key_name2"] = "value2";
string aValue = dict["key_name1"];

#3


4  

You could use a Dictionary:

可以使用字典 :

Dictionary dictiOnary= new Dictionary();
dictionary["key_name"] = "value1";

#4


2  

Try a dictionary:

试试一个字典:

var dictiOnary= new Dictionary();
dictionary.Add("key_name", "value1");

#5


0  

You can also use a KeyedCollection http://msdn.microsoft.com/en-us/library/ms132438%28v=vs.110%29.aspx where your value is a complex type and has a unique property.

您还可以使用KeyedCollection http://msdn.microsoft.com/en-us/library/ms132438%28v=vs.110%29.aspx,其中您的值是复杂类型并具有惟一属性。

You have your collection inherit from KeyedCollection, example ...

您的集合继承自KeyedCollection,例如……

    public class BlendStates : KeyedCollection
    {
    ...

This requires you to override the GetKeyForItem method.

这要求您重写GetKeyForItem方法。

    protected override string GetKeyForItem(BlendState item)
    {
        return item.DebugName;
    }

Then, in this example, the collection is indexed by string (the debug name of the BlendState):

然后,在本例中,集合被字符串(BlendState的调试名)索引:

    OutputMerger.BlendState = BlendStates["Transparent"];

#6


0  

Since everyone else said dictionary, I decided I would answer with 2 arrays. One array would be an index into the other.

既然大家都说字典,我决定用两个数组来回答。一个数组是另一个数组的索引。

You didn't really specify the data type that you would find at a particular index so I went ahead and chose string for my example.

您并没有指定在特定索引处可以找到的数据类型,因此我选择了string作为示例。

You also didn't specify if you wanted to be able to resize this later. If you do, you would use List instead of T [] where T is the type and then just expose some public methods for add for each list if desired.

您也没有指定是否要在以后调整大小。如果您这样做,您将使用List 而不是T[],其中T是类型,然后如果需要,只需公开一些公共方法来为每个列表添加。

Here is how you could do it. This could also be modified to pass in the possible indexes to the constructor or make it however you would do it.

这是你如何做的。还可以修改它,将可能的索引传递给构造函数,或者让它按照您希望的方式进行。

class StringIndexable
{
//you could also have a constructor pass this in if you want.
       public readonly string[] possibleIndexes = { "index1", "index2","index3" };
    private string[] rowValues;
    public StringIndexable()
    {
        rowValues = new string[ColumnTitles.Length];
    }

    /// 
    /// Will Throw an IndexOutofRange Exception if you mispell one of the above column titles
    /// 
    /// 
    /// 
    public string this [string index]
    {
        get { return getOurItem(index); }
        set { setOurItem(index, value); }


    }

    private string getOurItem(string index)
    {
        return rowValues[possibleIndexes.ToList().IndexOf(index.ToLower())];

    }
    private void setOurItem(string index, string value)
    {
        rowValues[possibleIndexes.ToList().IndexOf(index.ToLower())] = value;
    }

}

You would then call it like so :

你会这样称呼它:

  StringIndexable YourVar = new YourVar();
  YourVar["index1"] = "stuff";
  string myvar = YourVar["index1"];

推荐阅读
  • 本文由编程笔记#小编为大家整理,主要介绍了logistic回归(线性和非线性)相关的知识,包括线性logistic回归的代码和数据集的分布情况。希望对你有一定的参考价值。 ... [详细]
  • IB 物理真题解析:比潜热、理想气体的应用
    本文是对2017年IB物理试卷paper 2中一道涉及比潜热、理想气体和功率的大题进行解析。题目涉及液氧蒸发成氧气的过程,讲解了液氧和氧气分子的结构以及蒸发后分子之间的作用力变化。同时,文章也给出了解题技巧,建议根据得分点的数量来合理分配答题时间。最后,文章提供了答案解析,标注了每个得分点的位置。 ... [详细]
  • Oracle分析函数first_value()和last_value()的用法及原理
    本文介绍了Oracle分析函数first_value()和last_value()的用法和原理,以及在查询销售记录日期和部门中的应用。通过示例和解释,详细说明了first_value()和last_value()的功能和不同之处。同时,对于last_value()的结果出现不一样的情况进行了解释,并提供了理解last_value()默认统计范围的方法。该文对于使用Oracle分析函数的开发人员和数据库管理员具有参考价值。 ... [详细]
  • 本文介绍了一个适用于PHP应用快速接入TRX和TRC20数字资产的开发包,该开发包支持使用自有Tron区块链节点的应用场景,也支持基于Tron官方公共API服务的轻量级部署场景。提供的功能包括生成地址、验证地址、查询余额、交易转账、查询最新区块和查询交易信息等。详细信息可参考tron-php的Github地址:https://github.com/Fenguoz/tron-php。 ... [详细]
  • EPPlus绘制刻度线的方法及示例代码
    本文介绍了使用EPPlus绘制刻度线的方法,并提供了示例代码。通过ExcelPackage类和List对象,可以实现在Excel中绘制刻度线的功能。具体的方法和示例代码在文章中进行了详细的介绍和演示。 ... [详细]
  • 本文整理了315道Python基础题目及答案,帮助读者检验学习成果。文章介绍了学习Python的途径、Python与其他编程语言的对比、解释型和编译型编程语言的简述、Python解释器的种类和特点、位和字节的关系、以及至少5个PEP8规范。对于想要检验自己学习成果的读者,这些题目将是一个不错的选择。请注意,答案在视频中,本文不提供答案。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 本文介绍了[从头学数学]中第101节关于比例的相关问题的研究和修炼过程。主要内容包括[机器小伟]和[工程师阿伟]一起研究比例的相关问题,并给出了一个求比例的函数scale的实现。 ... [详细]
  • 在springmvc框架中,前台ajax调用方法,对图片批量下载,如何弹出提示保存位置选框?Controller方法 ... [详细]
  • 本文介绍了如何使用Express App提供静态文件,同时提到了一些不需要使用的文件,如package.json和/.ssh/known_hosts,并解释了为什么app.get('*')无法捕获所有请求以及为什么app.use(express.static(__dirname))可能会提供不需要的文件。 ... [详细]
  • IjustinheritedsomewebpageswhichusesMooTools.IneverusedMooTools.NowIneedtoaddsomef ... [详细]
  • 基于dlib的人脸68特征点提取(眨眼张嘴检测)python版本
    文章目录引言开发环境和库流程设计张嘴和闭眼的检测引言(1)利用Dlib官方训练好的模型“shape_predictor_68_face_landmarks.dat”进行68个点标定 ... [详细]
  • 欢乐的票圈重构之旅——RecyclerView的头尾布局增加
    项目重构的Git地址:https:github.comrazerdpFriendCircletreemain-dev项目同步更新的文集:http:www.jianshu.comno ... [详细]
author-avatar
澄墙_168
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有