草庐IT

构造器

全部标签

c# - 如何使静态构造函数成为非私有(private)的?

C#中的静态构造函数不允许使用public、private等访问修饰符。然而,VisualStudio代码分析在C#安全类别中有一个警告,显示“CA2121:Staticconstructorsshouldbeprivate”。是否可以使静态构造函数成为非私有(private)的?或者这是VisualStudio代码分析的错误?澄清:我并不是要让任何构造函数成为非私有(private)的。所以为什么?”问题无关紧要。我只是对两个Microsoft工具之间的矛盾感到好奇,想看看是否有任何我不知道的静态构造函数的处理方式。 最佳答案 C

c# - 为什么struct不能有无参数的构造函数

这个问题在这里已经有了答案:关闭13年前。为什么struct不能有无参数的构造函数?为CLR这样做有什么问题,或者为什么不允许这样做?请解释一下,我不明白。

c# - 实例构造函数中的 lock()

我在实例构造函数中的一些代码锁定语句中找到了。代码看起来像这样publicclassMyClass{privatereadonlyobject_syncRoot=newobject();publicMyClass(stringparameter){lock(_syncRoot){//somecode}}}这里我觉得lock是没有用的,因为这段代码不能在不同的线程中调用。每个线程都会创建自己的对象实例调用构造函数。但也许我错了,不知道什么。谢谢。编辑:在这个问题的第一个答案C#Amiusinglockcorrectly我找到了It'sbesttoputalockaroundthecode

c# - 有没有办法更改 Visual Studio 中 IntelliSense 中列出的构造函数的顺序?

我已经定义了一个具有多个构造函数的类,以便在对象实例化后底层接口(interface)是不可变的。当用户在VisualStudio中键入以下内容时,我希望其中一个构造函数成为“默认”构造函数:varobj=newMyClass(DimobjAsNewMyClass(目前,当我去实例化对象时,构造函数没有按照我在类中声明它们的顺序列出(在VisualStudioIntelliSense中)。有没有办法标记我的构造函数,以便它们的方法在VisualStudioIntelliSense中实例化期间以特定顺序出现? 最佳答案 没有办法在Vi

c# - Visual Studio 中的构造函数依赖代码片段

我发现自己向构造函数添加了很多依赖项,如下所示:publicclassSomeClass(){privateISomeService_service;privateIAnotherService_anotherService;publicSomeClass(ISomeServiceservice,IAnotherServiceanotherService){_service=service;_anotherService=anotherService;}}它们写起来很乏味,我一直在visualstudio中寻找代码片段来自动将一个添加到构造函数中,但没有找到。我想要的是:在向构造函数添

c# - Web API Controller 的构造函数是如何调用的?

根据thisarticle,Controller应该有一个构造函数来获取传入的要实现的接口(interface),ala:publicclassDuckbillsController:ApiController{IDuckbillRepository_platypiRepository;publicDuckbillsController(IDuckbillRepositoryplatypiRepository){if(platypiRepository==null){thrownewArgumentNullException("platypiRepositoryisnull");}_p

c# - '/' 应用程序中的服务器错误。没有为此对象定义无参数构造函数

调用堆栈显示如下:[MissingMethodException:Noparameterlessconstructordefinedforthisobject.]System.RuntimeTypeHandle.CreateInstance(RuntimeTypetype,BooleanpublicOnly,BooleannoCheck,Boolean&canBeCached,RuntimeMethodHandle&ctor,Boolean&bNeedSecurityCheck)+0System.RuntimeType.CreateInstanceSlow(BooleanpublicO

c# - 在哪里调用在构造函数中创建的 IDisposable 的 Dispose()?

在何处为对象拥有的IDisposable对象调用Dispose()?publicclassMyClass{publicMyClass(){log=newEventLog{Source="MyLogSource",Log="MyLog"};FileStreamstream=File.Open("MyFile.txt",FileMode.OpenOrCreate);}privatereadonlyEventLoglog;privatereadonlyFileStreamstream;//Othermembers,usingthefieldsabove}我应该实现Finalize()吗?对于

c# - 在 Unity 中注册类型时如何传入构造函数参数?

我在Unity中注册了以下类型:container.RegisterType,AzureTable>();AzureTable的定义和构造函数如下:publicclassAzureTable:AzureTableBase,IInitializerwhereT:TableServiceEntity{publicAzureTable():this(CloudConfiguration.GetStorageAccount()){}publicAzureTable(CloudStorageAccountaccount):this(account,null){}publicAzureTable(

C#静态字段、实例构造函数

我遇到了一个我想了解的C#行为。考虑这样一个类:publicclassSomeSingleton{publicstaticSomeSingletonDefault=newSomeSingleton();privatestaticintfield=0;privateSomeSingleton(){field=1;}publicintGetField(){returnfield;}}现在,让我们调用GetField()方法:varfield=SomeSingleton.Default.GetField();我得到0就好像跳过了实例构造函数一样。为什么? 最佳答案