小编Sae*_*eid的帖子

在C#中的数组列表中查找数组的索引

如果您有一个数组列表,如:

List<int[]> ListOfArrays = new List<int[]>();
ListOfArrays.Add(new int[] { 1, 1 });
ListOfArrays.Add(new int[] { 2, 1 });
Run Code Online (Sandbox Code Playgroud)

您如何在列表中找到{2,1}的索引?

我不想使用迭代循环.我想要一个简洁的方法,如PaRiMaL RaJ在Check中建议的方法是否字符串数组存在于字符串列表中:

list.Select(ar2 => arr.All(ar2.Contains)).FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)

(如果给定字符串数组的成员存在于字符串数组列表中,则上面的代码将返回true)

c# arrays list

7
推荐指数
1
解决办法
2388
查看次数

RawStringFormats 和 Delimeters 'pb' 在 clang 格式中的含义

我无法理解以下内容如何影响 clang 格式文件中的格式化行为:

RawStringFormats: 
  - Delimiters:       [pb]
    Language:        TextProto
    BasedOnStyle:    google
Run Code Online (Sandbox Code Playgroud)

这些设置对于 C++ 项目是否正确?(例如,语言不应该是 cpp 吗?)

分隔符的“pb”是什么意思,它与“cc”和“cpp”有什么不同?

我试图在 clang 文档中查找信息,但这只会增加我的困惑。

c++ clang-format

7
推荐指数
1
解决办法
204
查看次数

这是什么目的:"double x = x =(a - b)/(c - 1);"

我遇到了这样一个代码,在这个代码中,没有任何理由将变量赋给自己.

double x = x = (a - b) / (c - 1);
Run Code Online (Sandbox Code Playgroud)

这对我来说没什么意义.这背后有原因吗?

c#

6
推荐指数
1
解决办法
197
查看次数

`public string this [string columnName]`实现IDataErrorInfo接口时是什么意思?

public string this[string columnName]实现IDataErrorInfo接口时有什么意义?

    public string this[string columnName]
    {
        get
        {
            switch (columnName)
            {
                case "Name":
                    return ValidateName();
                case "PhoneNumber":
                    return ValidatePhoneNumber();
                default:
                    return string.Empty;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

我不明白为什么有方括号及其功能.


答: 感谢Hans和Scott现在我知道这只是索引器的语法.更多信息在这里.

c# wpf indexer idataerrorinfo

5
推荐指数
1
解决办法
3158
查看次数

如何从抽象类访问继承类的属性

我正在使用其中有一个抽象类的代码,例如:

public abstract class AbstractClass
{
   ...
}
Run Code Online (Sandbox Code Playgroud)

并且有从 AbstractClass 继承的类。根据来自继承类之一的用户输入创建一个对象。每个继承的类都有自己的属性:

class classOne : AbstractClass
    {
         ...
         public int A { get; set;}
         public int B { get; set;}
         public int C { get; set;}
         ...
    }


class classTwo : AbstractClass
    {
         ...
         public int D { get; set;}
         public int E { get; set;}
         ...
    }
Run Code Online (Sandbox Code Playgroud)

...假设我想在该代码中使用函数,并且我知道将返回哪种对象类型。由于程序的编写方式仅在程序运行时确定输出的类,因此如何修改输出对象的属性?

c#

4
推荐指数
1
解决办法
709
查看次数

将可分配变量传递给具有不可分配参数的子例程有什么影响?

假设我们有这个变量定义

Real*8, Dimension(:), Allocatable :: dblA
Allocate  (dblA(1000))
Run Code Online (Sandbox Code Playgroud)

现在我调用这个子程序:

Call MySub(dblA)
Run Code Online (Sandbox Code Playgroud)

其中:

Subroutine MySub(dblA)
Real*8,  INTENT(Out), DIMENSION(1000) :: dblA
End
Run Code Online (Sandbox Code Playgroud)

这种做法有什么副作用?我应该避免这个吗?

fortran

3
推荐指数
2
解决办法
243
查看次数

如何将一个方法应用于所有列表成员使用非常短的linq并省略lambda?

假设我有一个List,我想检查成员是否都等于"some string":

myList.All(v => v.Equals("some string"))

我想这会做同样的事情(或者会吗?!):

myList.All("some string".Equals)

但如果.Equals我不想使用自己的方法呢?

public bool LessThan16Char(string input)
{
    return (input.Length < 16);
}
Run Code Online (Sandbox Code Playgroud)

如何更换.Equals使用LessThan16Char


如果该方法具有第二个参数(例如lessthan(string Input, Int lessThanVal)),该怎么办

我还要感谢网上描述这些事情的任何文章,谢谢.

.net c# linq lambda method-group

2
推荐指数
1
解决办法
76
查看次数

这段代码中有+ =和 - =做什么?

我是wpf和mvvm概念的新手.这里有一个我正在研究的教程,但我无法理解这一部分; 在图7中:

 protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        MainWindow window = new MainWindow();

        // Create the ViewModel to which 
        // the main window binds.
        string path = "Data/customers.xml";
        var viewModel = new MainWindowViewModel(path);

        // When the ViewModel asks to be closed, 
        // close the window.
        EventHandler handler = null;
        handler = delegate
        {
            viewModel.RequestClose -= handler;
            window.Close();
        };
        viewModel.RequestClose += handler;

        // Allow all controls in the window to 
        // bind to the ViewModel by setting the …
Run Code Online (Sandbox Code Playgroud)

c#

1
推荐指数
1
解决办法
115
查看次数

创建列表的独立副本(而不是其中的对象)的最快方法是什么?

说我有一个ListA.我想制作一个重复的ListB,所以当我使用ListB.Remove(SomeItem);它时不会影响listA.

列表项应该引用相同的对象.

由于这是在循环中使用,因此使用资源要求最低的方法很重要.但我不知道如何以最有效的方式做到这一点.

c# list

0
推荐指数
1
解决办法
340
查看次数

为什么这个 Lambda 表达式被转换为 MemberExpression 和 UnaryExpression?

我是 C# 和 lambda 表达式的新手;在教程中,我无法理解代码对这个 Lambda 表达式的作用:

public ViewModel()
{
    base.AddRule(() => Aid, () =>
    Aid.Length >= (5 * 2) &&
    Aid.Length <= (16 * 2) &&
    Aid.Length % 2 == 0, "Invalid AID.");
}
Run Code Online (Sandbox Code Playgroud)

这是AddRule教程所说的将规则添加到规则字典中的方法:

public void AddRule<T>(Expression<Func<T>> expression, Func<bool> ruleDelegate, string errorMessage)
{
    var name = GetPropertyName(expression);

    ruleMap.Add(name, new Binder(ruleDelegate, errorMessage));
}
Run Code Online (Sandbox Code Playgroud)

protected static string GetPropertyName<T>(Expression<Func<T>> expression)
        {
            if (expression == null)
                throw new ArgumentNullException("expression");

            Expression body = expression.Body;
            MemberExpression memberExpression = body …
Run Code Online (Sandbox Code Playgroud)

c# lambda

0
推荐指数
1
解决办法
1143
查看次数

为什么在更改的字段上使用只读修饰符?

在Josh Smith的教程中,字段被定义为readonly:

    public class CustomerRepository
    {

    readonly List<Customer> _customers;
    ...
    public CustomerRepository(string customerDataFile)
    {
        _customers = LoadCustomers(customerDataFile);
    }
    ...
    }
Run Code Online (Sandbox Code Playgroud)

以后只读_customers更新列表:

    public void AddCustomer(Customer customer)
    {
        if (customer == null)
            throw new ArgumentNullException("customer");

        if (!_customers.Contains(customer))
        {
            _customers.Add(customer);

            if (this.CustomerAdded != null)
                this.CustomerAdded(this, new CustomerAddedEventArgs(customer));
        }
    }
Run Code Online (Sandbox Code Playgroud)

如何允许以及使用readonly有什么意义?

c# modifier

0
推荐指数
1
解决办法
67
查看次数

标签 统计

c# ×9

lambda ×2

list ×2

.net ×1

arrays ×1

c++ ×1

clang-format ×1

fortran ×1

idataerrorinfo ×1

indexer ×1

linq ×1

method-group ×1

modifier ×1

wpf ×1