本文介绍了全球变量替代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 您好, 我需要声明一个全局变量,例如:数据库连接句柄。所以 我在全班都能访问这个变量。我怎么能在c#中做到? 我是否必须将其声明为静态? 感谢您的帮助。Hello,I need to declare a global variable e.g. database connection handle. So thatI hava an access to this variable all over my class. How can I do it in c#?Do I have to declare it as static ?Thanks for any help.推荐答案 取决于,静态在一个类的所有实例之间共享一个变量。 你有一个数据库类的单个实例,你为每个数据库调用重用了 吗?在这种情况下,不需要静态。 每次调用数据库时,是否创建了数据库类的新实例?在这种情况下,你必须使用私人静态变量。 - MarcusAndrén - MarcusAndrénIt depends, static shares a variable between all instances of a class.Do you have a single instance of your database class that you reusefor each database call? In that case static isn''t needed.Do you create a new instance of your database class each time you callthe database? In that case you have to use a private static variable.--Marcus Andrén--Marcus Andrén 这篇关于全球变量替代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-22 18:22