你有静态构造函数的参数吗? [英] Can you have parameters for static constructors?

查看:154
本文介绍了你有静态构造函数的参数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我更新,我在采访中从这个问题中寻找很多时间

i m fresher i m seeking many time from this question in interview

推荐答案

没有。

试一试:你会得到编译错误:

No.
Try it: you will get a compilation error:
a static constructor must be parameterless



如果你考虑一下,你会怎么称呼带参数的静态构造函数?框架在幕后为您调用无参数版本...


And if you think about it, how would you call a static constructor with a parameter? The framework calls the parameterless version for you behind the scenes...


您不能使用参数创建静态构造函数。



您知道构造函数用于初始化局部变量或创建类实例。静态构造函数用于初始化静态变量而不是用于创建实例。



由于@OriginalGriff解释了框架调用静态构造函数的无参数版本,因此您无法传递参数意味着您无法控制静态构造函数。



例如假设你在C类中有两个静态属性(A和B),并且有一个没有任何参数的静态构造函数。现在你首先调用属性A,然后静态构造函数将被框架调用,然后在第二行代码中调用第二个属性B,然后静态构造函数不调用,因为它只调用一次。反之亦然。



所以你无法控制它。在Sort中,用户无法调用静态构造函数,因此他/她无法传递参数。最后静态构造函数不能重载。它只为一个类定义一次。
You can't create static constructor with parameter.

You know that constructor is used to initialize local variable or create class instance. Static constructor is used to initialize static variable not for create instance.

As @OriginalGriff explained that the framework calls the parameterless version of static constructor so you can't pass parameter means you don't have control on static constructor.

For example Suppose you have two static properties (A and B)in class C and have a static constructor that has n't any parameter. Now you first call property A then static constructor will be call by framework after that in second line of code you call second property B then static constructor does n't call because it calls only once. same as vice-versa.

So you don't have control it. In Sort user can't call static constructor so he/she can't pass parameter. finally Static constructor can't be overload. It define only once for one class.


这篇关于你有静态构造函数的参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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