引用类型对象没有更新,而是返回更新的对象

本文关键字:对象 更新 返回 引用类型 | 更新日期: 2023-09-27 18:12:21

我将"引用类型"传递给函数"BY VAL"。在函数中,我试图修改foreach循环中的引用类型。

当涉及到来电者时,我传递的object没有更新。但是返回更新后的对象。

我在下面给出了这个问题的例子。

谁能花几分钟时间告诉我为什么会这样?

下面是GetStudents(), FilterStudents(),班级,学生,老师,SSCClass,

SSCClass -包含教师数组。教师-包含学生数组。

    public void GetStudents()
    {
        Student[] sscStudentsOne = new Student[] { new Student { V_SId = 1, V_SName = "Kumar" }, new Student { V_SId = 2, V_SName = "Varun" }, new Student { V_SId = 3, V_SName = "Murthy" } };
        Student[] sscStudentsTwo = new Student[] { new Student { V_SId = 4, V_SName = "Sathya" }, new Student { V_SId = 5, V_SName = "Krishna" }, new Student { V_SId = 6, V_SName = "Bindu" } };

        Teacher[] sscTeachers = new Teacher[] { new Teacher { V_tId = 1, V_tName = "Jyothi", V_Students = sscStudentsOne }, new Teacher { V_tId = 2, V_tName = "Srinivas", V_Students = sscStudentsTwo } };
        SSCClass objSSCClass = new SSCClass();
        objSSCClass.V_sscTeachers = sscTeachers;
        SSCClass objSSCClassFiltered =  FilterStudents(objSSCClass);
        // Problem :-
        // in objSSCClass Kumar is getting removed but Sathya from second teachere is not getting removed.
        // in objSSCClassFiltered  Kumar and Sathya are Getting removed as we did.
        // We need objSSCClass object to be updated successfully ..  as we can not return this to caller method in my project.
    }
    public SSCClass FilterStudents(SSCClass objSSCClasstobeFiltered)
    {
        foreach (Teacher item in objSSCClasstobeFiltered.V_sscTeachers)
        {
            // here a method will be called and does the below
            // removes Students based on some criteria
            // Say,
            // Remove Student Kumar  from first teacher.
            // Remove Student Sathya from second teacher
        }
        // after filtering Students return the SSC Class object
        return objSSCClasstobeFiltered;
    }
    public class Student
    {
        int v_sId;
        public int V_SId
        {
            get { return v_sId; }
            set { v_sId = value; }
        }
        string v_SName;
        public string V_SName
        {
            get { return v_SName; }
            set { v_SName = value; }
        }
    }
    public class Teacher
    {
        int v_tId;
        public int V_tId
        {
            get { return v_tId; }
            set { v_tId = value; }
        }

        string v_tName;
        public string V_tName
        {
            get { return v_tName; }
            set { v_tName = value; }
        }

        Student[] v_Students;
        public Student[] V_Students
        {
            get { return v_Students; }
            set { v_Students = value; }
        }
    }

    public class SSCClass
    {
        Teacher[] v_sscTeachers;
        public Teacher[] V_sscTeachers
        {
            get { return v_sscTeachers; }
            set { v_sscTeachers = value; }
        }
    }

引用类型对象没有更新,而是返回更新的对象

你不能更新Foreach Ref,创建一个新的,然后更新它:

Foreach (Student s in student_list)
{...
   S s2 = s;
     s2 = newval;
...}