Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Scope of variables vs objects when parameterized into methods

Tags:

scope

c#

object

I make extensive use of inheritance, polymorphisim, and encapsulation but i just realised that i didn't know the following behavior about scope of an object vs a variable. The difference is best shown with code:

public class Obj
{
    public string sss {get; set;}

    public Obj()
    {
        sss = "0";
    } 
}     

public partial class testScope : System.Web.UI.Page
 {
    protected void Page_Load(object sender, EventArgs e)
    { 
        Obj a = new Obj(); 
        String sss = "0";            
        Context.Response.Write(sss); // 0

        FlipString(sss);
        FlipString(a.sss);
        Context.Response.Write(sss + a.sss); // 0 0

        FlipObject(a);
        Context.Response.Write(a.sss); // 1
        Context.Response.End();
    }

    public void FlipString(string str)
    { str = "1"; }

    public void FlipObject(Obj str)
    { str.sss = "1"; }
}

So I thought that when a variable is passed into a method that changes are limited to the scope of the method. But it appears that if an object is passed into a method that changes to it's properties extend beyond the method.

I could accept this if the rule was that this behavior exists for object and not variables but in .net everything is an object, a string (like the one in the example is System.String) So whats the rule, how can i predict the scope of a parameter that i pass into a method?

like image 818
missaghi Avatar asked Mar 08 '09 20:03

missaghi


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Why is C named so?

Quote from wikipedia: "A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix." The creators want that everyone "see" his language. So he named it "C".

What is C of computer?

" " C is a computer programming language. That means that you can use C to create lists of instructions for a computer to follow. C is one of thousands of programming languages currently in use.


1 Answers

You never pass an object as an argument - you only ever pass a reference or a value type value. Unless you use the ref keyword, arguments are passed by value - i.e. the initial value of the parameter is the evaluated value of the argument, and changes to the value of the parameter aren't seen by the caller.

However, it's very important to understand that the value of the parameter (for a reference type) is just the reference. If you change the contents of the object that the reference refers to, then that change will be seen by the caller.

It's a topic which deserves more than a few paragraphs - I suggest you read my article about parameter passing in .NET for more information.

like image 159
Jon Skeet Avatar answered Sep 21 '22 08:09

Jon Skeet