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. Any difference between returning an object and changing an object passed into a static function? [modified]

Any difference between returning an object and changing an object passed into a static function? [modified]

Scheduled Pinned Locked Moved C#
question
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.
  • X Offline
    X Offline
    xdavidx
    wrote on last edited by
    #1

    I was wondering if there was any difference between the following code: public void LoadAll() { obj x; //static function that will change the object StaticClass.DoSomething(x); } public void LoadAll() { //static function that will return a new object obj x = StaticClass.DoSomething(); } Are there any pros and cons to either of these? Are there any concerns with thread safety? -- modified at 22:55 Sunday 3rd June, 2007

    C 1 Reply Last reply
    0
    • X xdavidx

      I was wondering if there was any difference between the following code: public void LoadAll() { obj x; //static function that will change the object StaticClass.DoSomething(x); } public void LoadAll() { //static function that will return a new object obj x = StaticClass.DoSomething(); } Are there any pros and cons to either of these? Are there any concerns with thread safety? -- modified at 22:55 Sunday 3rd June, 2007

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Any concerns you may have remain the same in either case, ultimately I'd go with the second for clarity, but so long as it's all running in the same thread, which it is here, I don't see any thread safety concerns. The second approach does allow the method to create the object whenever it likes, the first means it always exists. The first would be better represented with the out keyword, seeing as you expect the value to be assigned, and not just read. Stupid C# doesn't have const, but you can make your code clear by using the out keyword.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      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