引用类型对象未更新,但返回更新的对象

时间:2013-05-05 04:07:32

标签: c#

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

然后说到来电者, 我通过的对象没有得到更新。 但是返回更新的对象。

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

有人可以花几分钟帮助我为什么会这样吗?

以下是方法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; }
        }

    }

1 个答案:

答案 0 :(得分:0)

您无法更新Foreach参考,创建一个新的,然后更新它:

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