我有一个类(SomeClass),其中包含一个属性Name的string类型.我需要存储该类的数组并按名称查找其项目.为此目的,有两种类型的集合:KeyedCollection和Dictionary.我的问题是:它们之间有什么区别,在这种情况下最好使用KeyedCollection和Dictionary?感谢您提供任何解释帮助.
我有一个泛型方法行为,其依赖于T是引用类型或值类型.它看起来如此:
T SomeGenericMethod <T> (T obj)
{
if (T is class) //What condition I must write in the brackets?
//to do one stuff
else //if T is a value type like struct, int, enum and etc.
//to do another stuff
}
Run Code Online (Sandbox Code Playgroud)
我不能复制这个方法,如:
T SomeGenericMethod <T> (T obj) where T : class
{
//Do one stuff
}
T SomeGenericMethod <T> (T obj) where T : struct
{
//Do another stuff
}
Run Code Online (Sandbox Code Playgroud)
因为他们的签名是平等的.谁能帮我?
我有这个代码:
string str = "valta is the best place in the World";
Run Code Online (Sandbox Code Playgroud)
我需要更换第一个符号.当我尝试这个:
str[0] = 'M';
Run Code Online (Sandbox Code Playgroud)
我收到了一个错误.我怎样才能做到这一点?
我有下一个代码:
struct T
{
public T(int u)
{
this.U = 10; //Errors are here
}
public int U { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)
C#编译器在规定的行中给出了一对错误:1)在将控制权返回给调用者之前,必须完全分配自动实现的属性"TestConsoleApp.Program.TU"的后备字段.考虑从构造函数初始化程序中调用默认构造函数.2)在分配所有字段之前,不能使用'this'对象
我做错了什么?帮帮我理解
我有一个类(控件),实现了ICustomTypeDescriptor,它在设计时和PropertyGrid运行时都用于自定义.我需要在设计时公开不同的属性(标准控件属性width,height等等),并在运行时,在我的程序中使用PropertyGrid来更改该控件的其他属性.
我的代码是这样的:
class MyControl : UserControl, ICustomTypeDescriptor
{
//Some code..
public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
return GetProperties();
}
public PropertyDescriptorCollection GetProperties()
{
//I need to do something like this:
if (designTime)
{ //Expose standart controls properties
return TypeDescriptor.GetProperties(this, true);
}
else
{
//Forming a custom property descriptor collection
PropertyDescriptorCollection pdc = new PropertyDescriptorCollection(null);
//Some code..
return pdc;
}
}
}
Run Code Online (Sandbox Code Playgroud)
C#中的设计时标志是否有模拟?使用条件编译可能更好吗?
我想在 gitlab ci/cd yml 文件的变量中使用数组,如下所示:
variables:
myarrray: ['abc', 'dcef' ]
....
script: |
echo myarray[0] myarray[1]
Run Code Online (Sandbox Code Playgroud)
但 Lint 告诉我该文件不正确:
variables config should be a hash of key value pairs, value can be a hash
Run Code Online (Sandbox Code Playgroud)
我已经尝试过下一个:
variables:
arr[0]: 'abc'
arr[1]: 'cde'
....
script: |
echo $arr[0] $arr[1]
Run Code Online (Sandbox Code Playgroud)
但构建失败并打印出 bash 错误:
bash: line 128: export: `arr[0]': not a valid identifier
Run Code Online (Sandbox Code Playgroud)
有没有办法在 .gitlab-ci.yml 文件中使用数组变量?
我需要使用命名事件来同步两个应用程序.但AutoResetEvent和ManualResetEvent都不包含带有事件名称的构造函数(仅初始状态).我可以通过AutoResetEvent或ManualResetEvent类中的静态方法OpenExisting打开现有的命名事件,但无法创建它!我不想为它使用原生的WinAPI CreateEvent函数,除非我确切地知道其他方法不存在.
我正在开发一个符合CLS的类型库,我在它里面有一个类,它包含私有,受保护和公共字段和属性.我使用下划线符号(_)作为私有或受保护字段的前缀,并使用少许第一个字母来区分具有相同名称的属性.它看起来如此:
class SomeClass
{
private int _age; //Here is OK
public int Age { get { return this._get; } }
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试使用受保护的字段时,我会遇到下一个问题:
class SomeClass
{
protected int _age; //Here is NOT CLS-compliant (because of _ can't be the first symbol of identifier)
public int Age { get { return this._get; } }
}
Run Code Online (Sandbox Code Playgroud)
然后我试着这样做:
class SomeClass
{
protected int age; //Here is NOT CLS-compliant (because of age and Age differ only in one symbol)
public int Age { get { …Run Code Online (Sandbox Code Playgroud) 我测试了这样的代码:
class A
{
public A() { }
public virtual void Test ()
{
Console.WriteLine("I am A!");
}
}
class B : A
{
public B() { }
public override void Test()
{
Console.WriteLine("I am B!");
base.Test();
}
}
class C : B
{
public C() { }
public override void Test()
{
Console.WriteLine("I am C!");
base.base.test(); //I want to display here "I am A"
}
}
Run Code Online (Sandbox Code Playgroud)
并尝试从C类方法调用A类(祖父母的方法).但它不起作用.请告诉我一种调用祖父母虚拟方法的方法.
我使用 TargetFramework .NET 5.0 (NET Core) 在 VS 2019 中创建了一个解决方案(包含 1 个项目)。我将 OutputPath(在项目属性中)设置为“bin\SomeMyFolder”,并期望所有输出文件都在那里。但事实证明,文件夹“ SomeMyFolde r”内部有另一个文件夹“ net5.0 ”,我的文件就在里面。我从来没有见过这样的自定义文件夹,是由 VS 以前使用其他 .Net Framework 版本创建的。有没有办法禁止 VS 2019 创建该文件夹并在我设置的该文件夹中构建我的项目(直接进入SomeMyFolder)?PS:请不要建议有关构建后事件的一些脚本,例如“copy SomeMyFolder\net5.0 -> SomeMyFolder | rmdir SomeMyFolder'
.net ×9
c# ×9
.net-5 ×1
constructor ×1
design-time ×1
generics ×1
gitlab ×1
gitlab-ci ×1
grandchild ×1
inheritance ×1
string ×1