所以这就是我在想的......
public class MyClass
{
public const string MyConstant = "MyConstantValue";
private static MyClass DefaultInstance;
static MyClass()
{
DefaultInstance = new MyClass();
}
}
...
NotificationService.RegisterForNotification(MyClass.MyConstant, Callback);
Run Code Online (Sandbox Code Playgroud)
这项工作还是我需要使用类似的东西 static readonly 属性 字段触发静态构造函数?
我有一个简单的类,它有一个静态构造函数和一个实例构造函数.现在,当我初始化类时,调用静态和实例构造函数.在应用程序域中只引用一次静态.我可以再次调用相同的类初始化和静态构造函数再次初始化吗?我试过但它没有发生?有没有什么办法可以在类上使用垃圾收集后在main()方法中再次调用静态构造函数.
这是代码:
public class Employee
{
public Employee()
{
Console.WriteLine("Instance constructor called");
}
static Employee()
{
Console.WriteLine("Static constructor called");
}
~Employee()
{
//Dispose();
}
}
Run Code Online (Sandbox Code Playgroud)
现在在main方法调用中:
static void Main(string[] args)
{
Employee emp = new Employee();
Employee emp = new Employee();
}
Run Code Online (Sandbox Code Playgroud)
输出:
名为Instance构造函数的静态构造函数称为Instance构造函数
现在静态没有再次调用.因为它在应用程序域中被调用一次.但他们是否可以在不卸载应用程序域的情况下再次调用它.我们可以在这里使用GC类吗?
谢谢.朋友
我有一类可以在类中保存的地图上执行的静态方法,我希望在调用类时设置地图.我尝试过使用私有的构造函数,但它没有被调用.我的代码的相关部分是:
public class MyClass
{
private static final String KEYS = "ABC";
private static final String[] DATA = {"AAA", "BBB", "CCC"};
private static HashMap<Character, String> myMap;
private MyClass() {
System.out.println("Running constructor");
populateMyMap();
}
private static void populateMyMap() {
myMap = new HashMap<Character, String>();
for (int i=0; i < KEYS.length; i++) {
myMap.put(KEYS.charAt(i), DATA[i]);
}
}
//various static methods
}
Run Code Online (Sandbox Code Playgroud)
私有构造函数是否适合在这里使用,如果是这样,我做错了什么?
对不起,如果这是重复的; 我试过寻找答案,但我不确定要搜索什么!
有这样的代码:
public static readonly bool MaximumRecipientsReached;
private static readonly IList<EmailAddress> Contacts;
static AdditionalRecipient()
{
Contacts = AnotherClass.Contacts; //works
}
public AdditionalRecipient()
{
MaximumRecipientsReached = true; //works not
}
Run Code Online (Sandbox Code Playgroud)
为什么我可以更改私有静态只读字段而不是公共字段?
PS:我当然正在使用属性.
这两个代码块都做同样的事情吗?
class A {
public static int s;
A(){}
static A(){s = 100;}
}
Run Code Online (Sandbox Code Playgroud)
和
class A {
public static int s=100;
A(){}
//static A(){s = 100;} do not use
}
Run Code Online (Sandbox Code Playgroud)
他们做同样的事情吗?我认同.
根据文档:
\n\n\n\n\n静态构造函数用于初始化任何静态数据,或执行只需执行一次的特定操作。它在创建第一个实例或引用任何静态成员之前自动调用。
\n
但我在 stackoverflow 帖子中看到了以下来自 C# 规范的引用:
\n\n\n\n\n如果类中存在静态构造函数 (\xc2\xa710.12),则在执行该静态构造函数之前立即执行静态字段初始值设定项。
\n
这是矛盾的,我不明白静态构造函数和静态成员初始化哪个先出现。
\n有没有一种标准的.NET类加载器工作方式?
说我编译这段代码:
Option Strict On : Option Explicit On
Module Module1
Sub Main()
System.Diagnostics.Debug.WriteLine("Main")
Dim g = C.A
End Sub
End Module
Public Class C
Shared Sub New()
System.Diagnostics.Debug.WriteLine("Init C")
End Sub
Shared Property A As New A
End Class
Public Class A
Shared Sub New()
System.Diagnostics.Debug.WriteLine("Init A")
End Sub
Public Sub New()
System.Diagnostics.Debug.WriteLine("A Constructor")
End Sub
End Class
Run Code Online (Sandbox Code Playgroud)
我可以保证编译后的代码(在所有实现的平台中)都有以下输出吗?
Main
Init A
A Constructor
Init C
Run Code Online (Sandbox Code Playgroud) 我试图访问静态列表的值.但是,当我尝试这样做时,抛出此异常.
System.TypeInitializationException:'Tips'的类型初始值设定项引发了异常. - > System.NullReferenceException:对象引用未设置为对象的实例
带有列表的类.
public static class Tips
{
//private List<Tip> roadtips = new List<Tip>();
public static List<Tip> tips { get; set; }
static Tips()
{
tips.Add(new Tip("Don't use your mobile phone whilst driving", "Making or receiving a call, even using a 'hands free' phone, can distract your attention from driving and could lead to an accident. "));
tips.Add(new Tip("Children", "Children often act impulsively, take extra care outside schools, near buses and ice cream vans when they might …Run Code Online (Sandbox Code Playgroud) 我正在学习Static Constructor在 C# 中发现的 C# 和 JAVA ,它们用于初始化任何静态数据,或执行只需要执行一次的特定操作。在创建第一个实例或引用任何静态成员之前自动调用它。
例如:
class SimpleClass
{
// Static variable that must be initialized at run time.
static readonly long baseline;
// Static constructor is called at most one time, before any
// instance constructor is invoked or member is accessed.
static SimpleClass()
{
baseline = DateTime.Now.Ticks;
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是我怎样才能在 JAVA 中获得相同的功能有什么办法???
我在尝试使用静态构造函数通过以下工厂注册我的类型时遇到问题:
public class Factory<T>
{
public static Factory<T> Instance { get { return _instance; } }
private static Factory<T> _instance = new Factory<T>();
private Factory() { }
static Factory() { }
static Dictionary<string, Type> _registeredType = new Dictionary<string, Type>();
public void Register(string id, T obj)
{
if (obj.GetType().IsAbstract || obj.GetType().IsInterface)
throw new ArgumentException("Cannot create instance of interface or abstract class");
_registeredType.Add(id, obj.GetType());
}
public T Create(string id, params object[] parameters)
{
Type type;
if(!_registeredType.TryGetValue(id, out type))
throw new UnsupportedShapeException(id);
return …Run Code Online (Sandbox Code Playgroud)