获取List C中的Array访问权限# [英] Geting access to Array in List C#

查看:58
本文介绍了获取List C中的Array访问权限#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好, 

我正在开展一个项目,但最简单的事情现在似乎很难,因此我需要你的帮助来解决这个代码块,我告诉你代码,然后我告诉你我面临的问题。

I am working on a project, but the most simpliest thing seems hard right now, and therefore I need your help to solve this code block, I show you the code and later on i tell you the problem I am facing with it.

while (loop)
            {
Console.WriteLine("\n\ttype title: ");
                            array[0] = Console.ReadLine();
                            Console.WriteLine("\n\tType Message: ");
                            array[1] = Console.ReadLine();
                            Lista.Add("\n\t" + DateTime.Now + "\n\tTitel: " + array[0]);
                            Lista.Add("\n\t" + "Message: " + array[1]);

if (str == array[0])
                                    {
                                    
                                        Console.WriteLine("\n\tFile found:\n" +
                                            
                                            "\n\tTitel: " + array[0] +
                                            "\n\tMessage: " + array[1]);
                                                           }


}

所以程序可以正常工作,它显示我在搜索文件标题时先前输入的内容。然而,问题是,当我查找范围大于2的文件时,当我搜索
的标题时,它将无法识别文件名。 

So the program works as it should, it shows me what i have typed earlier when i search for the title of the file. however, the problem is that when i look for files in wider range than 2 then it wont be able to recognize the file name when i am searching for the title. 

那么这笔交易是什么?你能输入解决问题的代码吗?

So what is the deal? Can you type a code that solves the problem?

foreach(string item in lista)
{Console.WriteLine(item)}

我知道你可以用它。

Comon需要你的帮助

Comon need your help here

谢谢

推荐答案

您可以创建一个用于保存数据的类,然后在其中进行搜索。尝试以下内容:

You can create a class for holding the data and then search in it. Try something like following:

public class Item
{

    public string Title { get; set; }
    public string Message { get; set; }

}

然后你可以按照以下方式填充它们:

and then you can populate them following way:

List<Item> items = new List<Item>();
while (loop)
{

    Item item = new Item();

    Console.WriteLine("\n\ttype title: ");
    item.Title = \n\t" + DateTime.Now + "\n\tTitle: " + 
    Console.ReadLine();
    
    Console.WriteLine("\n\tType Message: ");
    item.Message = "\n\t" + "Message: " + Console.ReadLine();

    items.Add(item);

}

然后你可以在列表中检查物品是否在那里:

and then you can check in the list if the item is there :

foreach(var element in items)
{
    if (str == element.Title)
    {
                                    
                                        
      Console.WriteLine("\n\tFile found:\n" +
                                            
                                            "\n\tTitel: " + element.Title +
                                            "\n\tMessage: " + element.Message);
    }


}

希望它有所帮助。


这篇关于获取List C中的Array访问权限#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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