Answered by:
ref var of object inside a method

Question
-
there is a program of a document manager that pushes the document to a Queue
There are 3 questions on 3 different threads, base on the same program
this is the THIRD question
the program is :-
using System; using System.Threading; using System.Threading.Tasks; using System.Collections.Generic; using System.Collections; using System.Linq; using System.Text; namespace scratchpad3 { class Program { public static void Main() { var dm = new DocumentManager<Document>(); //************************2 dm.AddDocument(new Document("Title A", "Sample A"));//**************4 dm.AddDocument(new Document("Title B", "Sample B")); dm.DisplayAllDocuments(); if (dm.IsDocumentAvailable) { Document d = dm.GetDocument(); Console.WriteLine(d.Content); Console.Read(); } } } // >>>>>>>>>>>>>>>>>>>>>>>>> decl of a class public class DocumentManager<T> where T : IDocument //************************1 { private readonly Queue<T> documentQueue = new Queue<T>(); public void AddDocument(T doc) //************************3 { lock (this) { documentQueue.Enqueue(doc); } } public bool IsDocumentAvailable { get { return documentQueue.Count > 0; } } public T GetDocument() { T doc = default(T); lock (this) { doc = documentQueue.Dequeue(); } return doc; } public void DisplayAllDocuments() { foreach (T doc in documentQueue) { Console.WriteLine(doc.Title); } } } //>>>>>>>>>>>>>>>>>>>>>>>>>>>>> decl of a class public interface IDocument { string Title { get; set; } string Content { get; set; } } public class Document : IDocument { public Document() { } public Document(string title, string content) { this.Title = title; this.Content = content; } public string Title { get; set; } public string Content { get; set; } } }
public void AddDocument(T doc) // //************************3
"T doc" may create a reference variable to an object called doc
but instead of passing a reference variable we are passing an object in the method call as:-
dm.AddDocument(new Document("Title A", "Sample A"));//************************4
etc ....
case1) Why this anomaly?
Tuesday, April 9, 2013 6:44 AM
Answers
-
dm.AddDocument(new Document("Title A", "Sample A"))
Here, there is nothing like passing the object to AddDocument function. Whenever you create an object like new Document(...) It always returns a reference to newly created object. In your case that reference is not stored anywhere but it is directly passed to AddDocument.
So, where is the anomaly.. right?
I hope this helps.
Please mark this post as answer if it solved your problem. Happy Programming!
- Marked as answer by amigo 1 Tuesday, April 9, 2013 1:36 PM
Tuesday, April 9, 2013 7:41 AM
All replies
-
thwe question did not print well so I give you this way case1) Why this anomaly? Case2) Can we intuitively know from a coding where it is a reference variable that is to be passed as a paremeter and where an object is to be passed as a parameter?
Tuesday, April 9, 2013 6:46 AM -
dm.AddDocument(new Document("Title A", "Sample A"))
Here, there is nothing like passing the object to AddDocument function. Whenever you create an object like new Document(...) It always returns a reference to newly created object. In your case that reference is not stored anywhere but it is directly passed to AddDocument.
So, where is the anomaly.. right?
I hope this helps.
Please mark this post as answer if it solved your problem. Happy Programming!
- Marked as answer by amigo 1 Tuesday, April 9, 2013 1:36 PM
Tuesday, April 9, 2013 7:41 AM