c语言函数输出输出型参数_c语言技巧多个输出参数

c语言函数输出输出型参数

Over the past few years under the “new Microsoft”, there have been many efforts to open-source the languages and frameworks that Microsoft develop. .NET Core (Microsoft’s development platform) is fully open-source and developers are actively encouraged to develop applications on non-Windows platforms such as Linux and macOS, something that wouldn’t have happened at all during the previous administration.

过去几年中,在“新Microsoft”的推动下,人们为开源Microsoft开发的语言和框架进行了许多努力。 .NET Core (Microsoft的开发平台)是完全开源的,并且积极鼓励开发人员在非Windows平台(例如Linux和macOS)上开发应用程序,这在上一届管理期间根本不会发生。

Anyway, I write this article while developing in Visual Studio Code on macOS and I came across a new C# feature that serves no other purpose than to put a smile on developers faces all around the globe: Value Tuples.

无论如何,我在macOS上使用Visual Studio Code进行开发时写了这篇文章,我偶然发现了一个新的C#功能,除了为全世界的开发人员带来笑容外,它没有其他目的:Value Tuples。

我为什么要在乎? (Why should I care?)

How many times have you needed to return 2 or more values from a method? Take this example:

您需要多少次从一个方法返回2个或多个值? 举个例子:

I need a method that generates a partition key and row key for a database from a given input value.

我需要一种根据给定的输入值为数据库生成分区键和行键的方法。

There are a few ways of doing this before tuple values came along. And, none of them are particularly pretty.

在元组值出现之前,有几种方法可以做到这一点。 而且,它们都不是特别漂亮。

输出参数 (Out Parameters)

A favourite of C/C++ developers, out parameters give you the ability to return values through method parameters (as opposed to using parameters for inputs).

out参数是C / C ++开发人员的最爱, out使您能够通过方法参数返回值(与将参数用于输入相反)。

public string GenerateKeys(string input, out string rowKey) {
// DO SOME WORK
rowKey = “Some new value”;
return “Some other value”;
}// Calling the method
var partitionKey = GenerateKeys("some input", out string rowKey);

In this example, I’m returning the partition key in the standard fashion but also return the rowKey using an out parameter. This is really ugly though and creates some inconsistencies. It also requires me to declare my partition and row key variables separately which isn’t pretty either. And, if the out parameter is optional, I need to implement and define it anyway or the compiler will get upset (and default out parameters aren’t supported).

在此示例中,我以标准方式返回分区键,但也使用out参数返回rowKey 。 但是,这确实很丑陋,并造成了一些不一致之处。 它还需要我分别声明分区和行键变量,这也不是一件好事。 而且,如果out参数是可选的,则无论如何我都需要实现和定义它,否则编译器会感到烦恼(并且不支持默认的out参数)。

Also, if you’re using async Task methods, out parameters don’t work!

另外,如果您使用异步Task方法,则out参数不起作用 !

Next!

下一个!

对象数组 (Object Arrays)

This option shouldn’t even be discussed, but, a developer I worked with several years ago did this and wasn’t ashamed at all, despite my (probably annoying) protests. I’m passionate, what can I say!

甚至不应该讨论这个选项,但是,尽管我(可能很烦人)提出抗议,但我几年前与一位开发商合作的开发人员做到了这一点,并且一点也不感到羞耻。 我很热情,我能说什么!

public object[] GenerateKeys(string input) {
// DO SOME WORK
return new object[] { "Hello", "World", 123, DateTime.Now};
}

Let’s all shudder in unison. But, why is this bad?

让我们齐声颤抖。 但是,为什么这样不好?

I speak often in this article about the joys of strict typing. When you don’t do this, you remove all changes of letting the compiler find the errors for you. Without these checks, dragons are almost certainly lurking around the corner.

在本文中,我经常谈到严格打字的乐趣。 如果不这样做,则将删除所有允许编译器为您找到错误的更改。 没有这些检查, 几乎可以肯定的是龙将潜伏在拐角处

This hack (scripting sellotape at best) completely removes any chance that the compiler has of finding your errors. And it makes it almost impossible for any other developer to correctly maintain your code.

这种技巧(最好是编写脚本脚本)完全消除了编译器发现错误的任何可能性。 而且这使得其他开发人员几乎不可能正确维护您的代码。

c语言函数输出输出型参数_c语言技巧多个输出参数_第1张图片
The gospel according to John [Woods] 约翰福音[Woods]

结构 (Structs)

You could declare your 2 output values in a struct and then return that. This is clean and well-typed (something C# developers love and JavaScript developers crave).

您可以在结构中声明2个输出值,然后将其返回。 这是干净且类型正确的(C#开发人员喜欢并且JavaScript开发人员渴望的东西)。

public DatabaseKey GenerateKeys(string input)
{
// DO SOME WORK
return new DatabaseKey {
RowKey = “Hello”,
PartitionKey = “World”
};
}public struct DatabaseKey
{
public string RowKey { get; set; }
public string PartitionKey { get; set; }
}

This is certainly cleaner and will work with async methods but is certainly a lot more code to write. I don’t really have anything against this method and it is much nicer than using out parameters, but anything that requires me to write more code than I need is never a good thing.

这肯定更干净,并且可以与异步方法一起使用,但是肯定要写很多代码。 我真的没有反对这种方法的任何东西,它比使用out参数要好得多,但是任何需要我编写比我需要的代码更多的代码从来都不是一件好事。

元组(旧方法) (Tuples (the old way))

Tuples have existed in C# since the dawn of Anders (well, C# 4.0 which came out 8 years after C# 1.0). System.Tuple is a simple class that acts as a container for up-to 8 generically defined arguments.

自安德斯 ( Anders)诞生以来,C#中就已经存在元组(好吧,C#4.0在C#1.0之后的8年问世)。 System.Tuple是一个简单的类,充当多达8个通用定义的参数的容器。

Tuple simply contains 2 values, Item1 is a string and Item2 is an int. Tuple contains 5 values etc…

Tuple仅包含2个值, Item1是字符串, Item2是int。 Tuple包含5个值,等等…

public Tuple GenerateKeys(string input) {
// DO SOME WORK
return new Tuple(“Hello”, “World”);
}var keys = GenerateKeys(phoneNumber);// Partition Key
keys.Item1// Row Key
keys.Item2

Again, this is compatible with async Tasks and is quite simple to write. But, what’s going on with the Item1 and Item2 code? Unlike the struct approach or the out params, the naming gives no clue as to what the purpose of the parameters are. This is fine on the day you write the code, but, what happens 2 years from now when you need to fix something?

同样,这与异步任务兼容,并且编写起来非常简单。 但是, Item1Item2代码是怎么回事? 与struct方法或out参数不同,命名不提供有关参数目的的任何线索。 在编写代码的那一天就可以了,但是,从现在起两年后,如果您需要修复某些问题,会发生什么?

This approach fortunately is still strictly typed (JavaScript?) so the compiler will catch you out if you try to use a string as an int for example. However, the naming is quite ugly.

幸运的是,这种方法仍然是严格类型(JavaScript?)的,因此,例如,如果您尝试使用字符串作为int类型,则编译器会将您赶上。 但是,命名很丑陋。

The other downside is that the Tuple class is a by-ref object which can cause some confusion (this topic is for another day and way beyond the scope of this article).

另一个缺点是,Tuple类是一个by-ref对象,可能会引起一些混淆(本主题讨论的时间超出了本文的讨论范围)。

我们快到了吗? (Are we nearly there yet?)

I’ve discussed 4 other ways of achieving the same goal. None of these are pretty and if you’re guilty of using object arrays, I have nothing nice to say to you.

我已经讨论了实现相同目标的其他4种方法。 这些都不是很漂亮,如果您对使用对象数组感到内gui,我无话可说。

This is where Value Tuples come in. Microsoft has recognised the need for making this well-trodden-path much easier for C# developers.

这就是Value Tuples的用武之地 。Microsoft已经认识到有必要使C#开发人员更轻松地使用这种广为人知的路径。

(string Partition, string Row) GenerateKeys(string phoneNumber) {
return ("Hello", "hash");
}var keys = GenerateKeys("input);keys.Partition
keys.Row

Rejoice! I’m using a statically typed return value which has names that are specific to my context. This comes with all of the benefits of the above but with no hideous code!

麾! 我正在使用一个静态类型的返回值,该返回值具有特定于我的上下文的名称。 它具有上述所有优点,但没有令人毛骨悚然的代码!

  • Strict Types — by declaring the strict return types of your tuple, the compiler is your friend and will stop you from doing stupid things

    严格类型-通过声明元组的严格返回类型,编译器是您的朋友,并且会阻止您执行愚蠢的事情

  • Clear Naming — by defining the names of your tuple values, code that uses this return value can know exactly what each field is for

    清除命名 -通过定义元组值的名称,使用此返回值的代码可以确切知道每个字段的用途

  • Async Support — you don’t need to do anything weird to support async Tasks

    异步支持 -您无需做任何奇怪的事情即可支持异步任务

  • Concise Code — no need to declare a long struct/class elsewhere in order to do this. Everything is inline

    简洁代码 -无需在其他地方声明长结构/类。 一切都是内联的

  • Nice and Clean — nuff said

    干净整洁 -纳夫说

This clearly is syntactic sugar. It adds no value to the performance of my software and is completely optional. However, there is a huge value here.

这显然是语法糖。 它对我的软件性能没有任何价值,并且是完全可选的。 但是,这里有巨大的价值。

By using these features I can keep my code much more concise yet still type-safe. Concise code can save many man-hours of debug time and your developers will thank you, trust me. If the code is simple to understand, you can squash bugs more quickly.

通过使用这些功能,我可以使我的代码更加简洁,但仍然是类型安全的。 简洁的代码可以节省大量的调试时间,您的开发人员将感谢您,相信我。 如果代码简单易懂,则可以更快地压缩错误。

As I’ve only just written the code using this new Tuple feature I have no idea if it will save me much time, but, anything that can potentially save me time in the future cannot be a bad thing.

由于我只是使用此新的Tuple功能编写了代码,所以我不知道它是否可以节省很多时间,但是,任何将来可能节省我时间的东西都不是一件坏事。

Well done Microsoft and the C# team. You allowed me a few hours out of my time to write this article. Hoorah.

微软和C#团队做得很好。 您花了我几个小时来写这篇文章。 呼啦。

翻译自: https://medium.com/the-innovation/c-language-tips-multiple-output-parameters-3180b806ced9

c语言函数输出输出型参数

你可能感兴趣的:(python,c语言,java,matlab,c++)