在C#中的静态变量 [英] Static variables in C#

查看:101
本文介绍了在C#中的静态变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,有没有把一个静态变量像VB.Net?



 静态MyCollection的作为一种方法一种方法收集


解决方案

为什么没有按'吨碳#支持静态方法
变量



问:在C ++中,有可能写一个
静态方法变量,并有
变量提供,只能从方法内部访问
。 C#不
提供了此功能。为什么?



答:有两个原因C#不
具有此功能。



首先,有可能由具有
类级的静态和添加方法
静获得近
中的相同的效果将需要增加
的复杂度。



二,方法级别静是
为造成
问题时,代码被称为
反复有些声名狼藉,或从多个线程,
和自定义都在
方法,它很难找到
定义



- MSDN C#常见问题



In C#, is there a way to put a static variable in a method like VB.Net?

Static myCollection As Collection

解决方案

Why doesn't C# support static method variables?

Q: In C++, it's possible to write a static method variable, and have a variable that can only be accessed from inside the method. C# doesn't provide this feature. Why?

A: There are two reasons C# doesn't have this feature.

First, it is possible to get nearly the same effect by having a class-level static, and adding method statics would require increased complexity.

Second, method level statics are somewhat notorious for causing problems when code is called repeatedly or from multiple threads, and since the definitions are in the methods, it's harder to find the definitions.

-- msdn c# faq

这篇关于在C#中的静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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