静态和默认构造函数 [英] Static and default constructor

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

问题描述

非静态类可以同时具有静态和默认构造函数。

A non static class can have static as well as default constructor at the same time.

这两个构造函数之间有什么区别?什么时候我只使用默认构造函数静态或静态?

What is the difference between these two constructors? When shall I go for only static or static with default constructor?

推荐答案

静态构造函数在访问实例之前对每个AppDomain运行一次的第一次。您可以使用它来初始化静态变量。

Static constructor runs once per AppDomain just before you access the instance of class first time. You can use it to initialize static variables.

另一方面,默认构造函数在每次创建类的新实例时运行。在默认构造函数中,您可以初始化实例的非静态字段。

On the other hand default constructor runs every time you create new instance of the class. in default constructor you can initialize non-static fields of the instance.

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

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