Zero exception in the program

I have a program like this

    class Program
    {
        static void Main(string[] args)
        {
            test objtest = new test();
            objtest.Name = "vikas";
            Test(objtest);
            //objtest = null; when I uncomment this line it shows me exception
            Console.WriteLine(objtest.Name);
            Console.ReadLine();
        }

        private static void Test(test objtest)
        {
            objtest.Name = "chetan";
            objtest = null;
        }
    }

    class test
    {
        private string _Name = string.Empty;
        public string Name
        {
            get
            {
                return _Name;
            }
            set
            {
                _Name = value;
            }
        }
    }

      

Conclusion: Chetan

Second program:

    class Program
    {
        static void Main(string[] args)
        {
            test objtest = new test();
            objtest.Name = "vikas";
            Test(objtest);
            objtest = null;
            try
            {
                Console.WriteLine(objtest.Name);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.InnerException + " " + ex.Message);
            }

            Console.ReadLine();
        }

        private static void Test(test objtest)
        {
            objtest.Name = "chetan";
            objtest = null;
        }
    }

    class test
    {
        private string _Name = string.Empty;
        public string Name
        {
            get
            {
                return _Name;
            }
            set
            {
                _Name = value;
            }
        }
    }

      

Output:

Object reference not to set an object instance

Why? When I set objtest = null;

to Test it shows me the value, but when I set to zero it shows me an error.

Added after @kmatyaszek's post:

In the first program

static void Main(string[] args)
        {
            test objtest = new test();
            objtest.Name = "vikas"; // **I am assigning this value**
            Test(objtest);
            //objtest = null; when I uncomment this line it shows me exception
            Console.WriteLine(objtest.Name);
            Console.ReadLine();
        }

        private static void Test(test objtest)
        {
            objtest.Name = "chetan";
            objtest = null;
        }

      

Why does it display "chetan" not "vikas" ??

0


source to share


3 answers


The classic confusion caused by a reference type being passed by value.

I'll give a rather short and simple answer here; those interested in learning more and in depth are more than happy to read Jon Skeet's article on Parameter Passing in C # and a similar article with Lee Richardson's diagrams.

A reference type, in short, is any type that is not primitive or structural. Any user defined class is therefore a reference type.

When an instance of such a class is passed to a function, what actually happens is that a pointer to that instance is passed. More precisely, a copy of the pointer is passed because, by default, parameters are passed by value.

If you have this line:

test objtest = new test();

      

A new instance of the class is created test

and a memory address is assigned. Every time you refer to a variable objtest

, this address will be used, for example:

objtest.Name = "vikas";

      

The engine will navigate to the address assigned when the instance was created, look for the space reserved for the property name, and change the content to "vikas". This is an instant and permanent change for this instance.

If you have a function signature like this:

private static void Test(test objtest)

      



The actual parameter passed behind the scenes is the address of the instance in memory. Whenever you refer to a parameter objtest

inside a function, the execution engine jumps to the address passed as the actual parameter. So, having this line inside a function:

objtest.Name = "chetan";

      

Just like outside the function: it will look for the space reserved for the property name in the memory address and change the contents there to "chetan". However, this change is immediate and permanent for this case. For this thing (changing properties) it doesn't matter if you use it ref

or not, as you are dealing with a reference type.

However , passed by value (for example, without a keyword ref

) means that the memory address is copied and the function only gets a copy, much like passing an integer. Any change to the copy will not affect the original value. Thus, when you have this line inside a function:

objtest = null;

      

You change the copy to dot for nothing, but the variable outside the function still points to the same address and won't be zero.

If you have a function signature like this:

private static void Test(ref test objtest)

      

This then means that the address itself is passed by reference, so changing the variable holding the address will cause it to change outside of the function.

This pretty much sums it up, I'm not bringing anything new here, just clarifying things with what I consider to be a simpler explanation.

+1


source


You have a problem passing a parameter to a function.

The default parameters are passed by value.

Attempting to reassign a parameter to a different memory location only works within the method Test

and does not affect the original variable objtest

in the Main method.

Therefore, when you add ref

to a parameter in a function Test

in two cases, the behavior will be the same, because all changes that occur inside the method Test

affect the original object objtest

in the Main method.



First example from your question with a ref parameter:

 class Program
    {
        static void Main(string[] args)
        {
            test objtest = new test();
            objtest.Name = "vikas";
            Test(ref objtest);
            //objtest = null; when I uncomment this line it shows me exception
            Console.WriteLine(objtest.Name);
            Console.ReadLine();
        }

        private static void Test(ref test objtest)
        {
            objtest.Name = "chetan";
            objtest = null;
        }
    }

    class test
    {
        private string _Name = string.Empty;
        public string Name
        {
            get
            {
                return _Name;
            }
            set
            {
                _Name = value;
            }
        }
    }

      

In the second example, first you are setting null to the original object, after which you want to read the property name, so you get NullReferenceException

.

0


source


Well, you are using an object with a value null

that throws an exception. null

for reference types means "points to zero". When you try to access the value stored at address zero, you will get NullReferenceException

.

You are setting objtest

in null

before you access the property Name

, so you are trying to access the property of the Name

object stored at address zero, which doesn't make sense.

0


source







All Articles