从C#字符串属性获取数组

编程入门 行业动态 更新时间:2024-10-28 08:19:40
本文介绍了从C#字符串属性获取数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在从数据库中检索一行,该行的属性是ID的逗号分隔列表

I'm retrieving a row from a database that has an attribute which is a comma separated list of ID's

1,2,3,4,5

在我的POCO中可以做这样的事情来找回阵列吗?

In my POCO is it possible to do something like this to get an array back?

public string SomeIDs { get { return SomeIDs.split(','); } set; }

很抱歉,为了澄清,我正在设置一个字符串,并想返回一个字符串数组

Sorry, to clarify, I am setting with a string and want to return a string array

推荐答案

您不能有一个接受string并返回string[]的设置器.您需要公开一个接受string的属性,以及一个只读属性(如下所示),该属性从该字符串返回已解析的数组:

You can't have a setter which accepts a string and returns a string[]. You'll need to expose one property which accepts a string, and a read-only property (as below) which returns a parsed array from that string:

private static readonly string[] emptyIds = new string[0]; public string SomeIds { get; set; } public string[] ParsedIds { get { return !string.IsNullOrEmpty(SomeIds) ? SomeIds.Split(',') : emptyIds; } }

更多推荐

从C#字符串属性获取数组

本文发布于:2023-11-06 04:33:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1562723.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   字符串   属性

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!