Skip to main content
deleted 20 characters in body
Source Link
paparazzo
  • 6k
  • 3
  • 19
  • 41
using System;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            NestedTest nt = new NestedTest();//create test
            nt.Navigate.Left();
            Console.WriteLine(nt.Position);
            nt.Navigate.Left();
            nt.Navigate.Left();
            Console.WriteLine(nt.Position);
            nt.Navigate.Rigth();
            Console.WriteLine(nt.Position);
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();



     
        }
    }
    class NestedTest
    {
        public int Position { get; private set; }
        public NavigateClass Navigate;//create the NavigateClass in NestedTest

        //Nested NavigateClass
        public class NavigateClass
        {
            private NestedTest _o;//the placeholder
            public NavigateClass(NestedTest obj)
            {
                _o = obj;//Set the NestedTest class so we know which Position we have to navigate trough.
            }
            public void Left()
            {
                Console.Write("<-");
                _o.Position--;//Move Position to the left (-1)
            }

            public void Rigth()
            {
                Console.Write("->");
                _o.Position++;//Move Position to the right (+1)
            }
        }

        public NestedTest(int num = 10)
        {
            this.Position = num;//Set start position
            Navigate = new NavigateClass(this);//Set the _o in Navigate so it knows which Position it has to navigate trough
        }
    }

}
using System;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            NestedTest nt = new NestedTest();//create test
            nt.Navigate.Left();
            Console.WriteLine(nt.Position);
            nt.Navigate.Left();
            nt.Navigate.Left();
            Console.WriteLine(nt.Position);
            nt.Navigate.Rigth();
            Console.WriteLine(nt.Position);
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();



 
        }
    }
    class NestedTest
    {
        public int Position { get; private set; }
        public NavigateClass Navigate;//create the NavigateClass in NestedTest

        //Nested NavigateClass
        public class NavigateClass
        {
            private NestedTest _o;//the placeholder
            public NavigateClass(NestedTest obj)
            {
                _o = obj;//Set the NestedTest class so we know which Position we have to navigate trough.
            }
            public void Left()
            {
                Console.Write("<-");
                _o.Position--;//Move Position to the left (-1)
            }

            public void Rigth()
            {
                Console.Write("->");
                _o.Position++;//Move Position to the right (+1)
            }
        }

        public NestedTest(int num = 10)
        {
            this.Position = num;//Set start position
            Navigate = new NavigateClass(this);//Set the _o in Navigate so it knows which Position it has to navigate trough
        }
    }

}
using System;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            NestedTest nt = new NestedTest();//create test
            nt.Navigate.Left();
            Console.WriteLine(nt.Position);
            nt.Navigate.Left();
            nt.Navigate.Left();
            Console.WriteLine(nt.Position);
            nt.Navigate.Rigth();
            Console.WriteLine(nt.Position);
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();    
        }
    }
    class NestedTest
    {
        public int Position { get; private set; }
        public NavigateClass Navigate;//create the NavigateClass in NestedTest

        //Nested NavigateClass
        public class NavigateClass
        {
            private NestedTest _o;//the placeholder
            public NavigateClass(NestedTest obj)
            {
                _o = obj;//Set the NestedTest class so we know which Position we have to navigate trough.
            }
            public void Left()
            {
                Console.Write("<-");
                _o.Position--;//Move Position to the left (-1)
            }

            public void Rigth()
            {
                Console.Write("->");
                _o.Position++;//Move Position to the right (+1)
            }
        }

        public NestedTest(int num = 10)
        {
            this.Position = num;//Set start position
            Navigate = new NavigateClass(this);//Set the _o in Navigate so it knows which Position it has to navigate trough
        }
    }

}
edited tags
Link
t3chb0t
  • 44.3k
  • 9
  • 84
  • 190
Source Link
Max R.
  • 137
  • 8

Nested Classes in C#

I wondered how nested classes in C# work as I often use them when I write native C# code (like Woods.Tree.Add(new Tree).Leaf.Color) so i tried it by my self with a Navigation example. There is a Position integer which declares a point on a line. To move left the position will be subtracted by 1, to move right the position will added by 1.

I think its quite pedantic and much code to write for such a 'simple' task. Is this the right way or is there an easier, smaller way to archive this?

using System;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            NestedTest nt = new NestedTest();//create test
            nt.Navigate.Left();
            Console.WriteLine(nt.Position);
            nt.Navigate.Left();
            nt.Navigate.Left();
            Console.WriteLine(nt.Position);
            nt.Navigate.Rigth();
            Console.WriteLine(nt.Position);
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();




        }
    }
    class NestedTest
    {
        public int Position { get; private set; }
        public NavigateClass Navigate;//create the NavigateClass in NestedTest

        //Nested NavigateClass
        public class NavigateClass
        {
            private NestedTest _o;//the placeholder
            public NavigateClass(NestedTest obj)
            {
                _o = obj;//Set the NestedTest class so we know which Position we have to navigate trough.
            }
            public void Left()
            {
                Console.Write("<-");
                _o.Position--;//Move Position to the left (-1)
            }

            public void Rigth()
            {
                Console.Write("->");
                _o.Position++;//Move Position to the right (+1)
            }
        }

        public NestedTest(int num = 10)
        {
            this.Position = num;//Set start position
            Navigate = new NavigateClass(this);//Set the _o in Navigate so it knows which Position it has to navigate trough
        }
    }

}