假设我有一个员工实例列表,employeeList
IEnumerator enumerator = employeeList.GetEnumerator();
while (enumerator.MoveNext())
{
Console.Write(enumerator.Current + " ");
}
Run Code Online (Sandbox Code Playgroud)
我有三个问题:
关于枚举器如何工作,我有一个很好的想法,就像C++中的迭代器一样.但是我不理解MoveNex方法(比如C++中的itr ++),因为它首先检查条件(是否在最后一个元素中),让我们说如果我们使用enumerator.Current来访问第一个元素,我认为它实际上当调用MoveNext()时,已经"移动"到列表中的下一个元素,因此当前指向的对象实际上是列表中的第二个元素?
我认为在使用enumerator.Current时我们可以访问当前元素是有意义的,例如,我们应该能够使用
enumerator.Current.name
就像我们可以在C++中使用(*itr).name或itr => name一样,但C#看起来并没有实现这种功能,那么使用enumerator.Current有什么意义呢?
IEnumerable<int> result = GetData() ?? Enumerable.Empty<int>;
作为C#的初学者,我只知道&&
,||
运营商,什么是" ??
"?
FILE * fPointer;
float amount = 3.1415;
fPointer =fopen("vending.txt","w");
fprintf(fPointer ,amount);
printf("The file has been created for the first time and we added the value %f" , amount);
fclose(fPointer);
Run Code Online (Sandbox Code Playgroud)
我正在尝试将浮点数保存到文本文件中,但是当我尝试运行此代码时,它会触发编译错误,因为函数 fprintf 期望第二个参数是一个字符数组,因此如何将浮点数转换为字符串我可以通过它,我来自 ac# 背景,在那里可以使用 .toString() 之类的东西,所以在 c 中有任何类似的东西可以直接将浮点数转换为字符串吗?
我定义了这个方法:
public static List<T2> ConvertList<T1, T2>(List<T1> param) where T1:class where T2:class
{
List<T2> result = new List<T2>();
foreach (T1 p in param)
result.Add((T2)p);
return result;
}
Run Code Online (Sandbox Code Playgroud)
用于将类型1的列表转换为类型2的列表.
不幸的是我忘记了,C#编译器在这个阶段不能说可以T1
转换为T2
,所以它抛出错误:
错误CS0030:无法将T1类型转换为T2
有人可以指导我如何正确地做到这一点?我现在需要这个方法只将自定义类的列表转换为列表object
,因此在.NET中所有内容都object
应该起作用.
基本上我所期望的是一些语法告诉编译器T2(object
)是T1(MyClass
)的基础,所以类似于:
public static List<T2> ConvertList<T1, T2>(List<T1> param) where T2: base of T1
Run Code Online (Sandbox Code Playgroud)
(... 其中T2:T1的基数)
I am trying to do some charting in LinqPad. I have some logs from an api and i know, what api was called and if the request was cached (in our real case we resolve address with coordinates from bing api or we get addres from cache table if we have cached it)
i use this linqpad script:
var startDate = new DateTime(2019, 1,1);
var Requests = new[]
{
new {Date=startDate, Name = "Api1", Cached=true },
new {Date=startDate, Name = …
Run Code Online (Sandbox Code Playgroud) 如何使用Roslyn解析C#条件编译语句.
在下面的代码中,我希望Roslyn提供Conditional编译语句节点.
public abstract class TestClass
{
public int Get()
{
#if DEBUG
return 1;
#else
return 2;
#endif
}
}
Run Code Online (Sandbox Code Playgroud)
我没有在SyntaxTree中获得条件编译节点,也没有它是LeadingTrivia }
或TrailingTrivia的一部分{
我得到的LeadingTrivia }
是"\t\t#endif\r\n\t\t"
和TrailingTrivia {
是"\r\n"
它是不完整的条件编译语句.
有人能指出我正确的方向吗?
美好的一天,
我需要创建将在存储变量名和变量的新值的Dictionary上迭代的函数.之后,我需要用该值更新类变量.
void UpdateValues(Type type, Dictionary<string, string> values)
{
foreach (var value in values)
{
var fieldInfo = selected.GetComponent(type).GetType().GetField(value.Key);
if (fieldInfo == null) continue;
fieldInfo.SetValue(selected.GetComponent(type), value.Value);
}
}
Run Code Online (Sandbox Code Playgroud)
它有效,但我想要一点改进,我绝对不知道是否可能.如您所见,该函数可以接受任何类,而不仅仅是一个类.
如果我有这样的课
class test
{
public string age;
}
Run Code Online (Sandbox Code Playgroud)
我会以这种方式使用函数,它会工作.
UpdateValues(typeof(test), new Dictionary<string, string>{{"age", "17"}});
Run Code Online (Sandbox Code Playgroud)
问题是,如果我有这样的类,我想更新"子字段"(字段中的字段)
class test
{
public string age;
}
class test2
{
public test data;
}
Run Code Online (Sandbox Code Playgroud)
我认为语法可能是这样的,但我不知道我怎么能这样做.
UpdateValues(typeof(test2), new Dictionary<string, string>{{"data.age", "17"}});
Run Code Online (Sandbox Code Playgroud)
总结一下,我需要创建一个函数来获取存储在另一个类中的类.函数将通过字典迭代并更新类甚至是子字段的字段.
我在MongoDB中有一组元素数组,如下所示:
{
"_id" : 5,
"quizzes" : [
{
"wk" : 1,
"score" : 10
},
{
"wk" : 2,
"score" : 8
},
{
"wk" : 3,
"score" : 5
}
],
"play" : [
{
"wk" : 2,
"score" : 8
},
{
"wk" : 3,
"score" : 5
}
]
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试在数组中插入新记录(如果不存在),如果该数组中存在记录,则更新该数组记录.下面是我的MongoDB查询.
db.push.update(
{ _id: 5 },
{ $push: { "quizzes": {"wk" : 6.0,"score" : 8.0},"play": {"wk" : 6.0,"score" : 8.0} } }
)
Run Code Online (Sandbox Code Playgroud)
每当我执行此查询时,它会在数组中插入新记录但我想如果记录存在则更新该数组.
在C#.net ConcurrentDictionary(C#参考源)的参考源代码中,我不明白为什么在以下代码片段中需要进行易失性读取:
public bool TryGetValue(TKey key, out TValue value)
{
if (key == null) throw new ArgumentNullException("key");
int bucketNo, lockNoUnused;
// We must capture the m_buckets field in a local variable.
It is set to a new table on each table resize.
Tables tables = m_tables;
IEqualityComparer<TKey> comparer = tables.m_comparer;
GetBucketAndLockNo(comparer.GetHashCode(key),
out bucketNo,
out lockNoUnused,
tables.m_buckets.Length,
tables.m_locks.Length);
// We can get away w/out a lock here.
// The Volatile.Read ensures that the load of the fields of 'n' …
Run Code Online (Sandbox Code Playgroud) 我有以下代码:
using MongoDB.Bson;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Bson.Serialization.Serializers;
using MongoDB.Driver;
using MongoDBTest;
using ServiceStack;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace protocol.server.API.Clients
{
public class ClientService : ServiceStack.Service
{
class CylinderSerializer : SerializerBase<Cylinder>
{
public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, Cylinder value)
{
var wr = context.Writer;
wr.WriteStartDocument();
wr.WriteName("_id");
wr.WriteObjectId(ObjectId.GenerateNewId());
wr.WriteName("description");
wr.WriteString(value.description.type);
context.Writer.WriteEndDocument();
}
public override Cylinder Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
{
context.Reader.ReadStartDocument();
Cylinder a = new Cylinder();
a.Id = context.Reader.ReadObjectId();
while (context.Reader.State != BsonReaderState.Type && context.Reader.ReadBsonType() != …
Run Code Online (Sandbox Code Playgroud) 我的数据库中有一个集合,用于记录事件。每种类型的事件都有一组不同的数据。我已经用以下类定义了它:
[CollectionName("LogEvent")]
public class LogEvent
{
public LogEvent(string eventType)
{
EventType = eventType;
EventData = new Dictionary<string, object>();
}
public string EventType { get; private set; }
[BsonExtraElements]
public IDictionary<string, object> EventData { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)
现在 - 这在某种程度上非常有效。只要EventData
字典的元素是简单类型...
var event = new LogEvent("JobQueues"){
EventData = new Dictionary<string, object>(){
{ "JobId": "job-123" },
{ "QueueName": "FastLane" }
}
}
_mongoCollection.InsertOne(event);
Run Code Online (Sandbox Code Playgroud)
...我得到 mongo 文件,如
{
_id: ObjectId(...),
EventType: "JobQueued",
JobId: "job-123",
QueueName: "FastLane"
}
Run Code Online (Sandbox Code Playgroud)
但是一旦我尝试将自定义类型添加到字典中,事情就会停止工作。
var event …
Run Code Online (Sandbox Code Playgroud)