简体   繁体   English

将某些 vb.net 代码转换为 c#

[英]Convert certain vb.net code to c#

I am converting a class library from vb.net to C#.我正在将类库从 vb.net 转换为 C#。 Everything is fine, but in one function that I am converting I am having trouble with it.一切都很好,但在我正在转换的一个函数中,我遇到了问题。

VB.NET Code VB.NET 代码

Protected Function CloseButtonOfTabPage(ByVal tp As TabPage) As PictureBox
    Return (From item In CloseButtonCollection Where item.Value Is tp Select item.Key).FirstOrDefault
End Function

Where CloseButtonOfTabPage is a System.Collection.Generic.Dictionary其中 CloseButtonOfTabPage 是 System.Collection.Generic.Dictionary

I am having trouble converting this code.我在转换此代码时遇到问题。 Can any body help me?有谁能够帮助我?

Assumed your dictionary has the correct types for key and value, this code in C# should do it:假设您的字典具有正确的键和值类型,C# 中的这段代码应该这样做:

protected PictureBox CloseButtonOfTabPage(TabPage tp)
{
   return CloseButtonCollection
          .Where(item => item.Value == tp)
          .Select(item => item.Key).FirstOrDefault();
}

(and you surely meant CloseButtonCollection is a dictionary, not CloseButtonOfTabPage ). (你肯定是说CloseButtonCollection是一个字典,而不是CloseButtonOfTabPage )。

Also a possible code sample: (using LINQ)还有一个可能的代码示例:(使用 LINQ)

protected PictureBox CloseButtonOfTabPage(TabPage tp)
{
    return (from item in CloseButtonCollection
            where item.Value == tp
            select item.Key).FirstOrDefault();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM