Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

Paste Description for Keyword usage: ref and out

From this question:

http://stackoverflow.com/questions/2058161/do-you-need-the-ref-or-out-parameter

Keyword usage: ref and out
Wednesday, January 13th, 2010 at 4:33:25pm UTC 

  1. using System;
  2.  
  3. namespace ConsoleApplication1
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             new Program().Run();
  10.  
  11.             Console.WriteLine("Press any key to exit . . .");
  12.             Console.ReadKey(true);
  13.         }
  14.  
  15.         public void Run()
  16.         {
  17.             Blah b = Dao.GetBlah(23);
  18.  
  19.             SomeService.ModifyXml(b);    // do I need to use out or ref here?
  20.             Dao.SaveXml(b.Xml);
  21.  
  22.             SomeService.SubstituteNew(out b);
  23.             Dao.SaveXml(b.Xml);
  24.            
  25.             SomeService.ReadThenReplace(ref b);
  26.             Dao.SaveXml(b.Xml);
  27.         }
  28.  
  29.         private class Blah
  30.         {
  31.             public int Id { get; set; }
  32.             public string Xml
  33.             {
  34.                 get
  35.                 {
  36.                     return "<Id>" + this.Id + "<Id>";
  37.                 }
  38.             }
  39.         }
  40.  
  41.         private static class Dao
  42.         {
  43.             public static Blah GetBlah(int id)
  44.             {
  45.                 return new Blah() { Id = id };
  46.             }
  47.  
  48.             public static void SaveXml(string blahXml)
  49.             {
  50.                 Console.WriteLine(blahXml);
  51.             }
  52.         }
  53.  
  54.         private static class SomeService
  55.         {
  56.             public static void ModifyXml(Blah b)
  57.             {
  58.                 b.Id = 17;
  59.             }
  60.  
  61.             public static void SubstituteNew(out Blah b)
  62.             {
  63.                 // The 'out' keyword means the parameter itself cannot
  64.                 // be read from until you have assigned a new value.
  65.  
  66.                 // Compile error on this line, uninitialized variable.
  67.                 //Console.WriteLine(b.Id);
  68.  
  69.                 // You cannot exit the method until you have assigned a new value.
  70.                 b = new Blah() { Id = 99 };
  71.             }
  72.  
  73.             public static void ReadThenReplace(ref Blah b)
  74.             {
  75.                 // The 'ref' keyword means you can replace
  76.                 //  the incoming reference with a new one.
  77.  
  78.                 // You can access the incoming reference immediately.
  79.                 Console.WriteLine(b.Id);
  80.  
  81.                 if (DateTime.Now.IsDaylightSavingTime())
  82.                 {
  83.                     // You are not required to replace the reference.
  84.                     b = new Blah() { Id = -18 };
  85.                 }
  86.             }
  87.         }
  88.     }
  89. }

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

comments powered by Disqus
worth-right
worth-right