C#如何删除数组中的用户 [英] C# how to delete user in array

查看:98
本文介绍了C#如何删除数组中的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户阵列`

i have a User's array`

User[] users = new User[]
        {
             new User ("Petros", "Petrosyan", 20),
             new User ("Poghos", "Poghosyan", 22),
             new User ("Valod", "Hakobyan", 23),
             new User ("Vazgen", "Hovhannisyan", 19),
             new User ("Ruben", "Martirosyan", 25),
        };



和类控制器,它有一个删除功能。当我打电话给我时,请告诉我删除main中的函数,例如users.delete(2);它将删除我的数组中的第二个索引元素,例如`new User(Valod,Hakobyan,23),



我尝试过的:



我试过这样的话`


and class Controller which has a Delete Function.Need me when i will call the delete function in main for example users.delete(2); it will delete second index's elements in my array, for example` new User ("Valod", "Hakobyan", 23),

What I have tried:

I tried like this`

public void delete(int index)
        {
            int current_index = 0;
            User[] nor_zang = new User[users.Length - 1];
            for (int i = 0; i < users.Length; i++)
            {
                if (i != index)
                {
                    nor_zang[current_index] = users[i];
                    current_index++;
                }
            }
            users = nor_zang;
            
        }



Main

Controller c = new Controller();
c.delete(2);



但它不起作用,这是我的错?


but it's not working, which is my fault?

推荐答案

可能问题是新控制器中没有任何项目 - 但是从这些代码片段中我们无法分辨。

所以这取决于你。从调试器开始,看看究竟发生了什么。

在函数的第一行放置一个断点,然后通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但是我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:debugging!
Probably, the problem is that the new controller doesn't have any items in it - but from those code fragments, we can't tell.
So it's going to be up to you. Start with the debugger, and look at exactly what is going on.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!


我用这段代码来找到问题(复制并粘贴到C#控制台应用程序项目中):



OriginalGriff是对的,如果您希望有人帮助您,应该明确说明它是什么没有工作,并给予足够的平衡来验证你在看什么 - 我只需要注释掉一行,并通过传入参数并返回结果稍微提高了删除功能的可读性...希望这有助于...



I used this code to find the problem (copy&paste into a C# Console App Project):

The OriginalGriff is right, if you expect someone to help you should give a clear statement of what it is that does not work and give enough evenidence to verify what you are looking at - I just had to comment out one line and slightly improved the readability of the delete function by passing in parameters and returning a result ...hope this helps ...

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

namespace ConsoleApp2
{
    public class User
    {
        public User(string name, string lastname, int age)
        {
            Name = name;
            LastName = lastname;
            Age = age;
        }

        public string Name { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }
    }

    class Program
    {
        public static User[] delete(int index, User[] users)
        {
            int current_index = 0;
            User[] nor_zang = new User[users.Length - 1];

            for (int i = 0; i < users.Length; i++)
            {
                if (i != index)
                {
                    nor_zang[current_index] = users[i];
                    current_index++;
                }
            }
////        users = nor_zang;

            return nor_zang;
        }

        static void Main(string[] args)
        {
            User[] users = new User[]
            {
                 new User ("Petros", "Petrosyan", 20),
                 new User ("Poghos", "Poghosyan", 22),
                 new User ("Valod", "Hakobyan", 23),
                 new User ("Vazgen", "Hovhannisyan", 19),
                 new User ("Ruben", "Martirosyan", 25),
            };

            var result = delete(2, users);
        }
    }
}


这篇关于C#如何删除数组中的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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