Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

c# - Best way to pass multiple keys and values together in a function?

I am a bit stuck here. I have a complicated API that was designed by a third party to retrieve User and Business data with custom search criteria. The search criteria syntax and format is not relevant but I am having to pass multiple keys and corresponding values. I need help creating the function.

Currently I have this function in my interface:

Response GetUser(
        List<string> keys,
        List<string> values,
        List<string> fieldsToReturn= null
        );

What's the best way to pass multiple keys and values where they can be tied/mapped together? And as one parameter? Key and value must match in count 1:1. Value is a search value that is tied to its corresponding key.

I already have pretty good validation with the current setup that it works okay, but I'm reconsidering and wanting to make key and value passed together to avoid too many unforseen input validation issues.

question from:https://stackoverflow.com/questions/65914728/best-way-to-pass-multiple-keys-and-values-together-in-a-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I would use the value tuple syntax

GetUser(IEnumerable<(string Key, string Value)> keyValues)

This provides guaranteed matching between key values. I think it also communicates better that this is not intended for lookups, as a dictionary would.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...