扩展方法嵌套静态类不允许? [英] Extension methods not allowed in nested static classes?

查看:127
本文介绍了扩展方法嵌套静态类不允许?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是为什么?我会觉得它的真正的高兴能够有锁定了一些扩展方法,只能在我的班级之一使用。真的不希望有一定的可扩展方法无处不在......他们看起来比普通的静态方法好得多:P

Why is this? I would find it really nice to be able to have some extension methods locked down to be used only within one of my classes. Don't really want to have certain extension methods available everywhere... and they look so much nicer than regular static methods :P

有关澄清:

我希望这些扩展方法的原因是因为我伸出的形式,上有一个DataGridView。而且我已经非常累了这样的诗句中:

The reason I want these extension methods is because I am extending a Form, which has a DataGridView on it. And I am getting very tired of lines like these:

foreach(var row in grid.Rows.OfType<DataGridViewRow>().Where(r => (bool) r.Cells[checkBoxColumn.Index].Value))

foreach(var row in grid.SelectedRows.OfType<DataGridViewRow>().Where(r => (bool) r.Cells[checkBoxColumn.Index].Value))

想延长方法,这样我可以只是做

Would like an extension method so that I could just do

foreach(var row in grid.Rows.CheckedRows())

foreach(var row in grid.SelectedRows.CheckedRows())

所以,换句话说,这(在grid.SelectedRows.CheckedRows()VAR行)扩展方法可能没有用处都超出这一类。但它会使代码变得更干净。当然也可以进行定期的方法也是如此,而这正是我落得这样做,因为这是不可能的。

So in other words, this extension method would not be useful at all outside this class. But it would make the code a lot cleaner. Can of course make regular methods too, and that is what I ended up doing, since this wasn't possible.

Aaanyways,我只是想知道如果任何人有一些很好的论据为什么他们选择把在那里扩展方法可以用来创建这样的限制。的必须在一个静态类的非常有意义。的无法在一个嵌套的静态类的不...至少对我来说...

Aaanyways, I was just curious to know if anyone had some good arguments to why they had chosen to put a restriction like this upon where extension methods can created used. Must be in a static class makes total sense. Can't be in a nested static class doesn't... to me at least...

推荐答案

应该使用扩展方法真正使你的代码的大量的清洁的比使用标准的辅助方法,在你的类?

Would using an extension method really make your code a lot cleaner than using a standard helper method on your class?

// extension method
foreach (var row in grid.Rows.CheckedRows())
foreach (var row in grid.SelectedRows.CheckedRows())

// standard helper method
foreach (var row in CheckedRows(grid.Rows))
foreach (var row in CheckedRows(grid.SelectedRows))

这篇关于扩展方法嵌套静态类不允许?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆