Wrox Programmer Forums
|
C# 4.0 aka C# 2010 General Discussion Discussions about the C# 4.0, C# 4, Visual C# 2010 language and tool not related to any specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 4.0 aka C# 2010 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 28th, 2012, 02:21 AM
Authorized User
 
Join Date: Sep 2012
Posts: 29
Thanks: 14
Thanked 0 Times in 0 Posts
Default LinkedList on page 110 C#4 2010 Wrox

Code:
// this program is the program on page 110 C#4 2010 Wrox
// it deals with the creation of a linkedlist-----.
using System;
using System.Collections;
using System.Linq;
using System.Text;

namespace scratch_pad_2
{
    class Program
    {

        static void Main(string[] args)
        {
            var list1 = new LinkedList();
            list1.AddLast(2);
            list1.AddLast(4);
            list1.AddLast(6);
            foreach (int i in list1)
            {
                Console.WriteLine(i);
                Console.Read();
            }
        }
    }

    public class LinkedListNode 
    {
        public LinkedListNode(object value)
        {
            this.Value = value;
        }
        public object Value { get; private set; }
        public LinkedListNode Next { get; internal set; }
        public LinkedListNode Prev { get; internal set; }
    }
     public class LinkedList 
   // public class LinkedList : IEnumerable
    //     note that both the above statements are valid
    {
        public LinkedListNode First { get; private set; }
        public LinkedListNode Last { get; private set; }
        public LinkedListNode AddLast(object node)
        {
            var newNode = new LinkedListNode(node);
            if (First == null) { First = newNode; Last = First; }
            else
            { Last.Next = newNode; Last = newNode; }
            return newNode;        
        }
        public IEnumerator GetEnumerator()
        {
            LinkedListNode current = First;
// current here is an identifier/variable

            while (current != null) { yield return current.Value;
            current = current.Next;
            }
        }
        
    }
}


Q1 ] How does the internal keyword work here?
Q2] How to make use of the Prev property?
Q3] In this statement :
public object Value { get; private set; }
why the type is “object” instead of “ LinkedListNode “
Q4] Should we remember such programs ?
Q5] Is there any class called “LinkedList” ? I know there is a class called “LinkedList<T>” , so that the transition to such a class (“LinkedList<T>” ) in the next section of the chapter would create a system class overlap , --- actually If you may please explain this area
 
Old November 28th, 2012, 03:48 AM
Wrox Author
 
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

Quote:
Originally Posted by amigo1 View Post



Q1 ] How does the internal keyword work here?
Q2] How to make use of the Prev property?
Q3] In this statement :
public object Value { get; private set; }
why the type is “object” instead of “ LinkedListNode “
Q4] Should we remember such programs ?
Q5] Is there any class called “LinkedList” ? I know there is a class called “LinkedList<T>” , so that the transition to such a class (“LinkedList<T>” ) in the next section of the chapter would create a system class overlap , --- actually If you may please explain this area
That many questions ;-)
Q1: internal works here the same as on other places. Using internal with the set accessor of the property, only code within the assembly is allowed to access this property accessor.
Q2: Do you mean this?
Code:
var node = myLinkedList.Prev;
Q3: Because the Value property is used to access the object that is kept with the LinkedListNode.
Q4: why not? Of course it's better to use generic types instead of object types, but I guess this sample leads you in that way and makes a comparision later on.
Q5: There is a LinkedList<T>, as you know already. There is no overlap as one type is non-generic, and the other one is generic, and they are also in different namespaces. Of course as there is a LinkedList<T> class in the Framework there's no need to create a custom one. However, it's good to know how this works to build something similar, and to know what's behind the LinkedList<T> class.
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel
The Following User Says Thank You to ChristianNagel For This Useful Post:
amigo1 (November 30th, 2012)
 
Old November 30th, 2012, 02:29 AM
Authorized User
 
Join Date: Sep 2012
Posts: 29
Thanks: 14
Thanked 0 Times in 0 Posts
Default

thank you teacher --- its been a long time that we had spoken
how are you?
regards
amigo1





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 3, Page 110 ntonline1 BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 1 March 2nd, 2011 10:06 AM
Cannot open WROX.Suggestion in VS 2010 leisenstein BOOK: Professional DotNetNuke 5: Open Source Web Application for ASP.NET ISBN: 978-0-470-43870-1 2 February 23rd, 2011 01:19 AM
What's behind the 'Equal' and 'LinkedList' buttons Everard Hughes BOOK: Professional Visual Basic 2008 ISBN: 978-0-470-19136-1 1 September 27th, 2008 08:44 AM
multi-leveled linkedlist saiyedriyaz BOOK: Beginning Java 2 1 May 29th, 2004 03:00 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.