Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. About string ,and help me to solution one problom about zhe value and reference type! Thank you! [modified]

About string ,and help me to solution one problom about zhe value and reference type! Thank you! [modified]

Scheduled Pinned Locked Moved C#
help
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gjllyl
    wrote on last edited by
    #1

    hi, zhe subject is that class Class1 { private string str = "class1.str"; private int i = 0; static void StringConvert(string str) { str = "string being converted."; } static void StringConvert(Class1 c) { c.str = "string being converted."; } static void Add(int i) { i++; } static void AddWithRef(ref int i) { i++; } static void Main() { int i1 = 10; int i2 = 20; string str = "str"; Class1 c = new Class1(); Add(i1); AddWithRef(ref i2); Add(c.i); StringConvert(str); StringConvert(c); Console.WriteLine(i1); Console.WriteLine(i2); Console.WriteLine(c.i); Console.WriteLine(str); Console.WriteLine(c.str); Console.Read(); } tell me why "Console.WriteLine(str);" and "Console.WriteLine(c.str);" zhe two sentence have two different results! I know that zhe "string" is reference type! but here i am puzzled!

    modified on Saturday, June 20, 2009 8:09 AM

    G 1 Reply Last reply
    0
    • G gjllyl

      hi, zhe subject is that class Class1 { private string str = "class1.str"; private int i = 0; static void StringConvert(string str) { str = "string being converted."; } static void StringConvert(Class1 c) { c.str = "string being converted."; } static void Add(int i) { i++; } static void AddWithRef(ref int i) { i++; } static void Main() { int i1 = 10; int i2 = 20; string str = "str"; Class1 c = new Class1(); Add(i1); AddWithRef(ref i2); Add(c.i); StringConvert(str); StringConvert(c); Console.WriteLine(i1); Console.WriteLine(i2); Console.WriteLine(c.i); Console.WriteLine(str); Console.WriteLine(c.str); Console.Read(); } tell me why "Console.WriteLine(str);" and "Console.WriteLine(c.str);" zhe two sentence have two different results! I know that zhe "string" is reference type! but here i am puzzled!

      modified on Saturday, June 20, 2009 8:09 AM

      G Offline
      G Offline
      Gideon Engelberth
      wrote on last edited by
      #2

      While you are correct that string is a reference type, you cannot change which string the local variable str in Main is referencing from the StringConvert function since you pass it by value. All you can do is make the parameter to StringConvert refer to another string, which will have no effect outside the function. In the case where you passed a Class1, you are modifying the reference contained in the object referred to by the parameter, which is the same object referred to in Main. If you had put the line c = new Class1(); as the first line in StringConvert, it would no longer change the value in Main because the parameter in StringConvert would now be referring to a different object.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups