如何让构造函数在我的C#程序中返回值。 [英] How Can I Make A Constructor To Return Value In My C# Program.

查看:581
本文介绍了如何让构造函数在我的C#程序中返回值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让构造函数在我的C#程序中返回值。

How can i make a constructor to return value in my C# program.

推荐答案

构造函数不能有返回类型。你甚至不能使用虚空。使用带有ref关键字的参数构造函数。
Constructor can not have return type. You can't even use void. Use parameter constructor with ref keyword instead.


你不能。

构造函数是一种已经有返回值的特定类型的方法 - 新实例您正在创建的课程。



您可以传递 ref out 构造函数的参数,但这很不寻常:

You can't.
A constructor is a specific type of method that already has a return value - the new instance of the class you are creating.

You could pass a ref or out parameter to the constructor, but that would be rather unusual:
public class MyClass
    {
    static int instanceCount = 0;
    public MyClass(ref int val)
        {
        val = ++instanceCount;
        }
    }


构造函数不支持任何返回类型。甚至无效。默认情况下,隐式返回类型是声明它的类类型。你可以使用OriginalGriff提到的'ref'或'out'参数。
A constructor does not support any return type. Not even void. The implicit return type by default is the class type in which it is declared. You can use 'ref' or 'out' parameter as mentioned by OriginalGriff.


这篇关于如何让构造函数在我的C#程序中返回值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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