我已经阅读了关于Directory.EnumerateFiles和之间差异的讨论Directory.GetFiles。
我知道他们在内部都使用
System.IO.FileSystemEnumerableFactory.CreateFileNameIterator()
不同之处在于EnumerateFiles可能会使用延迟执行(lazy),而会使用GetFiles()a ToArray,因此该函数已经被执行了。
但是如果在迭代过程中将文件和文件夹添加到字典中会发生什么。迭代是否只迭代在EnumerateFiles()?期间存在的项目?
更糟糕的是:如果在迭代过程中删除文件会发生什么:它们还会被迭代吗?
简化:在我的数据库中,我有一种产品在不同日期以不同价格出售。换句话说,它有一个价格历史记录。我有两个类:具有一对多关系的产品和价格:
public class Product
{
public int ProductId {get; set;}
public string Name {get; set;}
public ICollection<Price> Prices {get; set;}
}
public class Price
{
public int PriceId {get; set;}
// foreign key to Product:
public int ProductId {get; set;}
public Product Product {get; set;}
public DateTime ActivationDate {get; set;}
public decimal value {get; set;}
}
public class MyDbContext : DbContext
{
public DbSet<Price> Prices { get; set; }
public DbSet<Product> Products { get; set; …Run Code Online (Sandbox Code Playgroud) 简短的介绍:
我有一个带有DataGridView的UserControl。我想向设计者公开DataGridView Columns集合,因此我可以在设计时更改用户控件上的列。
问:为此,我需要哪些设计师属性?
对于那些对较长版本感兴趣的人:
我有一个具有以下功能的UserControl:
该用户控件可以自主运行。它具有父控件要使用的一个功能:
UserControl引发两个事件:
我必须以几种形式显示此用户控件。唯一的区别是每个表格的DataGridViewColumn集合不同。
我可以以编程方式添加列,但是使用设计器创建列会更容易。
c# user-controls datagridview windows-forms-designer winforms
我在使用Clear()从实体框架中的集合中删除所有元素时遇到问题
考虑一下常用的博客和帖子示例.
public class Blog
{
public int Id {get; set;}
public string Name {get; set;}
public virtual ICollection<Post> Posts { get; set; }
}
public class Post
{
public int Id { get; set; }
// foreign key to Blog:
public int BlogId { get; set; }
public virtual Blog Blog { get; set; }
public string Title { get; set; }
public string Text { get; set; }
}
public class BlogContext : DbContext
{
public DbSet<Blog> Blogs …Run Code Online (Sandbox Code Playgroud) 我注意到Dictionary Class实现IDictionary<TKey,?TValue>以及IReadOnlyDictionary<TKey,?TValue>.
但是IDictionary 接口没有实现IReadOnlyDictionary<TKey,?TValue>.
这意味着如果我的类有一个IDictionary<TKey,?TValue>, 并且我需要传递一个 ,IReadOnlyDictionary<TKey,?TValue>我应该为此创建一个适配器,或者使用不同的解决方法。
恕我直言,每个具有 an 功能的类IDictionary<TKey,?TValue>也具有IReadOnlyDictionary<TKey,?TValue>.
他们是否只是忘记了实现它,或者是否有真正有意义的类实现IDictionary<TKey,?TValue>而不能具有有意义的实现IReadOnlyDictionary<TKey,?TValue>?
我想创建一个泛型类,其中类的类型可以解析字符串.
我想用这个类有一个静态函数解析(串)的任何类,如System.Int32,System.Double,也为喜欢的System.Guid类.它们都具有静态Parse功能
所以我的类需要一个where子句,它将我的泛型类型约束为具有Parse函数的类型
我想像这样使用它:
class MyGenericClass<T> : where T : ??? what to do ???
{
private List<T> addedItems = new List<T>()
public void Add(T item)
{
this.AddedItems.Add(item);
}
public void Add(string itemAsTxt)
{
T item = T.Parse(itemAsTxt);
this.Add(item);
}
}
Run Code Online (Sandbox Code Playgroud)
在where子句中写什么?
我们都知道System.IDisposable模式。在StackOverflow上已经描述了无数的时间:
Disposable模式建议仅在处置对象时才处置托管资源,而不是在完成过程中处置
您可以看到这是因为建议使用以下代码:
protected void Dispose(bool disposing)
{
if (disposing)
{
// Code to dispose the managed resources of the class
}
// Code to dispose the un-managed resources of the class
}
Run Code Online (Sandbox Code Playgroud)
我知道,只要我的班级具有实现System.IDisposable的(私有)成员,我的班级就应该实现System.IDisposable。如果布尔处理为true,则Dispose(bool)应调用私有成员的Dispose()。
如果在完成过程中调用Dispose,为什么会出现问题?那么,如果在完成过程中调用以下Dispose会导致问题呢?
protected void Dispose(bool disposing)
{
if (myDisposableObject != null)
{
myDisposableObject.Dispose();
myDisposableObject = null;
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个Person类,并创建了一个从EqualityComparer <Person>派生的相等编译器类。但是默认的EqualityComparer不会调用我的相等比较器的Equals函数
根据MSDN EqualityComparer <T> .Default属性:
Default属性检查类型T是否实现System.IEquatable接口,如果是,则返回使用该实现的EqualityComparer。否则,它返回一个EqualityComparer,它使用T提供的Object.Equals和Object.GetHashCode的替代。
在下面的(简化)示例中,Person类未实现工具System.IEquatable <Person>。因此,我希望PersonComparer.Default将返回PersonComparer的实例。
但是没有调用PersonComparer.Equals。没有调试输出,返回值为false。
public class Person
{
public string Name { get; set; }
}
public class PersonComparer : EqualityComparer<Person>
{
public override bool Equals(Person x, Person y)
{
Debug.WriteLine("PersonComparer.Equals called");
return true;
}
public override int GetHashCode(Person obj)
{
Debug.WriteLine("PersonComparer.GetHasCode called");
return obj.Name.GetHashCode();
}
}
public static void Main()
{
Person x = new Person() { Name = "x" };
Person y = new Person() { Name = "x" };
bool …Run Code Online (Sandbox Code Playgroud) 我有一个ReturnStatementDetailsForSubRepAsync使用 linq 表达式的函数 ( ),我可以使用诸如.ToListAsync(). 现在因为这个 LINQ 函数是异步的,所以我必须使父函数异步。
父函数如下所示:
public async Task<IEnumerable<StatementDetail>> ReturnStatementDetailsAsync(string cid, string userName, int statementNo)
{
var statementDetails = new List<StatementDetail>;
if (HttpContext.Current.User.IsInRole(UserLevel.Subrep.GetDescription()) || HttpContext.Current.User.IsInRole(UserLevel.SubRepMaster.GetDescription()))
{
var subRepStmtDetails = await ReturnStatementDetailsForSubRepAsync(cid, userName, statementNo); //Linq query with ToListAsync()
foreach (var item in subRepStmtDetails)
{
statementDetails.Add(new SubRepStatementDetailItem(item));
}
}
else
{
var regionalStmtDetails = await Task.Run(() => StoredPrcedureAsyncTest(cid, statementNo); //Entity framework stored procedure call
foreach (var item in regionalStmtDetails)
{
statementDetails.Add(new RegionalStatementDetailItem(item));
}
}
return …Run Code Online (Sandbox Code Playgroud) 我有一个相当简单的生产者-消费者模式,其中(简化)我有两个生产者,他们生产由一个消费者消费的输出。
为此,我使用 System.Threading.Tasks.Dataflow.BufferBlock<T>
一个BufferBlock对象被创建。一个Consumer是听这个BufferBlock,并处理任何接收到的输入。
send data to the同时有两个“生产者BufferBlock”
简化:
BufferBlock<int> bufferBlock = new BufferBlock<int>();
async Task Consume()
{
while(await bufferBlock.OutputAvailable())
{
int dataToProcess = await outputAvailable.ReceiveAsync();
Process(dataToProcess);
}
}
async Task Produce1()
{
IEnumerable<int> numbersToProcess = ...;
foreach (int numberToProcess in numbersToProcess)
{
await bufferBlock.SendAsync(numberToProcess);
// ignore result for this example
}
}
async Task Produce2()
{
IEnumerable<int> numbersToProcess = ...;
foreach (int numberToProcess in numbersToProcess)
{
await bufferBlock.SendAsync(numberToProcess);
// ignore result for …Run Code Online (Sandbox Code Playgroud) c# dataflow producer-consumer task-parallel-library tpl-dataflow
c# ×10
.net ×1
asynchronous ×1
dataflow ×1
datagridview ×1
dictionary ×1
disposable ×1
dispose ×1
enumerate ×1
file ×1
generics ×1
getfiles ×1
icollection ×1
idisposable ×1
linq ×1
many-to-many ×1
tpl-dataflow ×1
where-clause ×1
winforms ×1