C#VS Python的参数传递 [英] C# vs Python argument passing

查看:232
本文介绍了C#VS Python的参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有哪些主要的区别,如果有的话,Python的参数传递规则VS C#的参数传递规则?

我很熟悉Python,只有开始学习C#。我想知道,当一个对象被引用或值同为C#,因为它是用Python,或者有我需要记住一些关键的差别。

解决方案

除非你指定你想要它不同的C#流逝值参数。如果参数类型是一个结构,它的值复制,否则参考对象被复制。这同样适用于返回值。

您可以使用 REF 退出修饰符,它都必须在方法声明中指定修改此行为而在方法调用。这两种改变行为的参数来传递通过引用。这意味着你不能再通过在更复杂的前pressions。 REF 和退出是传递一个变量为 REF时的区别C $ C>参数,它必须已经被同时一个变量传递给退出参数没有被初始化。在该方法中,退出参数被视为未初始化的变量并在返回之前必须赋值。

What are the main differences, if any, of Python's argument passing rules vs C#'s argument passing rules?

I'm very familiar with Python and only starting to learn C#. I was wondering if I could think of the rule set as to when an object is passed by reference or by value the same for C# as it is in Python, or if there are some key differences I need to keep in mind.

解决方案

C# passes parameters by value unless you specify that you want it differently. If the parameter type is a struct, its value is copied, otherwise the reference to the object is copied. The same goes for return values.

You can modify this behavior using the ref or out modifier, which must be specified both in the method declaration and in the method call. Both change the behavior for that parameter to pass-by-reference. That means you can no longer pass in more complex expressions. The difference between ref and out is that when passing a variable to a ref parameter, it must have been initialized already, while a variable passed to an out parameter doesn't have to be initialized. In the method, the out parameter is treated as uninitialized variable and must be assigned a value before returning.

这篇关于C#VS Python的参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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