Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9176165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:02:46+00:00 2026-06-17T17:02:46+00:00

I have a program like this class Program { static void Main(string[] args) {

  • 0

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;
            }
        }
    }

Output:
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 instance of object

Why?
When I set objtest = null; in Test it shows me the value, but when I set null in same it shows me error.

Added after @kmatyaszek post:

In 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 it is displaying “chetan” not “vikas”??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T17:02:47+00:00Added an answer on June 17, 2026 at 5:02 pm

    Classic confusion caused by reference type that is being passed by value.

    I will give a rather short and simple answer here; those interested to learn more and in depth are more than welcome to read Jon Skeet article on Parameter passing in C# and similar article with diagrams, by Lee Richardson.

    Reference type, in short, is any type that is not primitive or struct. Any custom defined class is hence a reference type.

    When instance of such class is passed to a function, what actually happens is that a pointer to this instance is passed. More accurately, a copy of the pointer is being passed since by default parameters are passed by value.

    When you have this line:

    test objtest = new test();
    

    New instance of the class test is being created and assigned address in the memory. Every time you refer to the variable objtest, that address will be used for example:

    objtest.Name = "vikas";
    

    The runtime engine will go the address assigned when the instance was created, look for the place reserved for the property Name and change the contents there to “vikas”. The change is immediate and permanent for this instance.

    When you have such function signature:

    private static void Test(test objtest)
    

    The actual parameter that is passed “behind the scenes” is the address of the instance in the memory. Whenever you refer to the parameter objtest inside the function, the runtime engine will go to the address passed as the actual parameter. So having this line inside the function:

    objtest.Name = "chetan";
    

    Is exactly the same as having it outside the function: it will look for the place reserved for the property Name in the memory address passed, and change the contents there to “chetan”. Yet again, this change is immediate and permanent for that instance. For this thing (changing properties) it doesn’t matter if you’re using ref or not, as you are dealing with reference type.

    However, being passed by value (e.g. without ref keyword) means that the memory address is being copied and the function only get the copy, very much like passing integer. Any change to the copy will not affect the original value. Thus, when you have this line inside the function:

    objtest = null;
    

    You change the copy to point on nothing, however the variable outside the function still point to the same address and won’t be null.

    If you have such function signature:

    private static void Test(ref test objtest)
    

    Then it means the address itself is passed by reference, hence changing the variable holding the address will cause it to be changed outside the function as well.

    This pretty much sums it up, I don’t bring anything new here just clarifying things with what I deem more simple explanation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

lets say I have something like this: class Program { static void Main(string[] args)
I have this class: public class Test { public static void main(String[] args) {
I have the following code: class Program { static void Main(string[] args) { Test
class Program { static void Main(string[] args) { //Period 01 2008, Period 02 2008
I have a program like this - import weka.core.stemmers.SnowballStemmer; public class TestProject{ public static
I have a piece of code like this: Class Program { static StreamReader sr
So if i have something like this: public class TestClass { public static void
I have a program like this public class no_of_letters_count { static int i; public
I have a windows mobile app that look like this: class Program { static
i have a progam like this class A { public void test1(String s1) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.