我是c#的新手。请指导我找出这个问题的解决办法。我有一个静态类A,里面有一个静态构造函数。守则如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
}
}
static class A
{
我对C#规范很好奇,它说明了在C# 5 (.net4)中初始化静态字段的顺序。例如:
public class Test
{
public static readonly string A = "hi";
public static readonly string B = "bye";
public static readonly string DEFAULT = A;
}
在测试(Mono2.x)中,它们似乎是按照它们在代码中出现的顺序初始化的。例如:实际上,DEFAULT的值是"hi",但是如果我将DEFAULT的定义移到A
这些天我只是在做一些关于OOPS的头脑风暴,突然有一个问题出现在我的脑海中。我发现这是相关的,所以我决定问community.Question:
静态构造函数不能具有返回类型(int、string等),但静态方法在C#中必须具有返回类型。
c#编译器如何区分这两种情况以使其通过编译?
static Class staticClass
{
public static staticClass(){} //right
public static int staticClass(){} //wrong
public static int staticMethod(){} /
大家好,我是c#语言新手,我正在使用vb.net vb.net代码:
Module Module1 ' main static class
Sub Main()
' output: constructor will be excuted
End Sub
Sub New()
Console.WriteLine("constructor will be excuted")
End Sub
End Module
但是,当我使用c#时,类程序的默认构造函数永远不会执行
class Program
{
我对visual studio的intellisense有一个问题。每次我在结构中创建C#静态构造函数时,visual studio的智能感知都会在尝试调用构造函数时中断。它似乎找不到构造器,甚至找不到默认的构造器。有人知道我为什么会有这个问题吗?
public Triangle(int aX, int aY, int bX, int bY, int cX, int cY)
{
A = new Point(aX, bY);
B = new Point(bX, bY);
C = new Point(cX, cY);
}
我对Java很陌生,但我在C#上工作了多年。所以我要把用Java编写的代码翻译成C#。一切都还好,但我想出了一个似乎很奇怪的密码。以下是代码:
//Java Version
public class ParentClass{
ArrayList<SomeType> values;
void MyFunction(){ /*some codes*/ }
//other codes
public class NestedClass {
public void handleMessage(SomeType val) {
我想要实现的是
// Base class
public Class ActiveRecord {
public static string CollectionName;
public static Collection() {
return CollectionInstance(CollectionName); // just pseudo code
}
}
// Child class
public Class Post : ActiveRecord
{
public static string CollectionName = "pos
C#中的类怎么可能没有定义构造函数呢?例如,我有一个类
internal class TextStyle
{
internal string text = "";
internal Font font = new Font("Arial", 8);
internal Color color = Color.Black;
}
在代码中,这个类被实例化为
TextStyle textParameters = new TextStyle();
每个人都说静态初始化器是线程安全的,但我担心的是一个特别的细节。
假设我有
static class MyStaticClass
{
public static readonly object myField = MyOtherClass.GetNewObject();
}
static class MyOtherClass
{
public static object GetNewObject()
{ /* arbitrary code that returns a new object */ }
}
当C#尚未初始化时,MyStaticClass.myField保
在我的众多主题中,有一次是为了在C#中找到一个很好的静态覆盖解决方案(目标C很简单,所以不要告诉我“这是不可能的”,或者说“只有对象是多态的”,因为我知道),我尝试了一种委托方法。我的代码是:
public class Animal
{
public static Func<string> Name { get; protected set; }
static Animal()
{
Name = () => "Animal";
}
}
public class Cat : Animal
{
stati
当我试图为预期条件类创建一个实例时,它会抛出一个错误。
ExpectedConditions obj = new ExpectedConditions();
这里我遇到的错误是“没有构造函数定义的”。
ExpectedConditions是一个密封类。而驻留在密封类中的方法是静态方法。
在C#类中,默认情况下存在一个构造函数。只有静态类没有默认构造函数。
所以我尝试了一个小例子
public sealed class A
{
public static string GetName()
{
return "name
我很抱歉,如果这是一个新手的问题,但我找不到任何类似的搜索S.O。我继承了一个小的C#程序,以增强,它主要是一个DLL。
我用一种伪代码来表示DLL,以解释我的问题: DLL有4个类,3个“普通”类,第4个类只是公共静态变量,这些变量被DLL的其余部分用作全局变量。
public class CMain
{
public CMain ()
{
CFoo Foo = new CFoo(); // CFoo's c'tor is called
CBar Bar = new CBar(); // CBar
考虑到下面的_userDataStorage代码,我预计私有数据成员C#将立即初始化。相反,我发现它根本没有被初始化。我在初始化语句上放置了一个断点,它永远不会被击中。这意味着DB静态属性始终返回NULL。静态类是否与非静态类不同?
public static class UserDataStorageWrapper
{
private static UserDataStorage _userDataStorage = new UserDataStorage();
public static UserDataStorage DB
{
get
我需要在代码和中实现一个似乎符合我的需求的单例模式。
public sealed class Singleton
{
private static readonly Singleton instance = new Singleton();
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static Singleton()
{
}
private Singleton()
{
}
public static Singleton Instance
{
为了更好地理解property和static property,我正在编写一小段代码。如下所示:
class UserIdentity
{
public static IDictionary<string, DateTime> OnlineUsers { get; set; }
public UserIdentity()
{
OnlineUsers = new Dictionary<string, DateTime>();
}
}
或
class UserIdentity
{
public IDictionary&
我不明白C#中的这个错误
错误CS0236:字段初始化程序不能引用非静态字段、方法或属性'Prv.DB.getUserName(long)‘
对于以下代码
public class MyDictionary<K, V>
{
public delegate V NonExistentKey(K k);
NonExistentKey nonExistentKey;
public MyDictionary(NonExistentKey nonExistentKey_) { }
}
class DB
{
SQLiteConnection
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
B b = new B();
public class A
{
static A()
{
Console.WriteLine("Static ctor A");
}
public A()
{
Console.WriteLine("ctor A");
}
}
public
public sealed class Singleton
{
Singleton() {}
public static Singleton Instance
{
get
{
return Nested.instance;
}
}
class Nested
{
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
我正在用VB.NET重写一个C#类。C#类中有一个静态构造函数:
static Class()
{
...
}
通过调用栈,我发现它是由主窗口的InitializeComponent()方法调用的。我看过的所有地方都告诉我,VB.NET中对应的静态构造函数就是:
Shared Sub New()
...
End Sub
但是这个方法永远不会被调用。我创建的VB.NET静态构造函数是正确的吗?是不是其他的东西和我的静态构造函数没有关系?
我遇到了一种我想要理解的C#行为。
为什么在这两种情况下都先运行实例构造函数?
class Program
{
public static void Main()
{
//Singleton s = new Singleton(); case 1
var test = Singleton.Instance; // case 2
}
}
class Singleton
{
static readonly Singleton _instance =