从程序返回列表(新手) [英] return a list from program (newbeginner)

查看:121
本文介绍了从程序返回列表(新手)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我是C#的新手
我还不了解所有事物如何相互连接,例如类和对象...

我创建了一个简单的项目.
我的目标是拥有两个不同的公共班级.

myfirst类(包含并列出"companyInfo")

mySecondClass(调用companyinfo列表)

从静态void Main(string [] args)中警报companyInfo中的所有项目

有人可以帮我吗?

请参阅下面的代码:

Hello People,

I am total new to C#
I haven''t understood how everything is connected to each other classes and objects etc...

I have created a a simple project.
My goal it is to have two different public classes.

myfirst class(contains and list "companyInfo")

mySecondClass (calls the companyinfo list)

from static void Main(string[]args) alert all items in the companyInfo

Could someone help me?

see my code below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace MyProgram
{
     }
     class Program
    {
        static void Main(string[] args)
        {
            {

                CompanyMain CCinfo = new CompanyMain();         
           
            }
        }


        public class myList
        {
            static List<string> CompanyInfo()
            {
                List<string> CompanyInformation = new List<string>();
                CompanyInformation.Add("test1");
                return CompanyInformation;
            }
        }

        public class CompanyMain
        {

            myList CompanyInfo = new myList();
            
        }

     }



在此先谢谢您



Thank you in advance

推荐答案

对不起,我的英语...

首先,您需要更改公司信息"字段的访问修饰符,因为未指定时,默认情况下将其设为私有,并且您无法在类中看到它.

Sorry for my english...

First you will need to change access modifier of Company Info field, because when it isn''t specified, by default take private and you can''t see it out the class.

public class CompanyMain
        {
 
           public myList Company<big></big>Info = new myList();
            
        }




之后,主要可以看到CompanyInfo字段,并且可以对其进行操作.




After that, in the main you can see the CompanyInfo field and you can manipulate it.

static void Main(string[] args)
       {
           {
               CompanyMain CCinfo = new CompanyMain();
           foreach(var item = CCinfo.CompanyInfo.CompanyInfo()){
                 //

             }
           }
       }


为了循环浏览列表中的所有项目,您需要使用 foreach [^ ]语法.

例如

In order to cycle through all the items in a list you need to use the foreach[^] syntax.

e.g.

foreach s in CCinfo.CompanyInfo()
 ''do something with the company info here...
next


这篇关于从程序返回列表(新手)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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