Enums debugging and Nested types in C#

Goud.Kv
Posted by in C# category on for Beginner level | Points: 250 | Views : 5270 red flag
Rating: 5 out of 5  
 1 vote(s)

C# is an Object-oriented programming language. C# comes with simplicity, expressiveness and great performance to meet the programmer productivity.
Recommendation
Read Enums and properties of enums in C# before this article.

Introduction

In the previous chapter, we have seen enums with examples in C#. Now, lets see debugging enums and nested types in C# in this chapter.

Objective

The main objective of this article is to learn debugging with enums and nested types in C# programming.

Debugger (Enums)

We already know what is enum in C#. Now, we are going to test the enums that how it looks like in the debugger of Visual Studio.

Let's have a simple code,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Example
{
    class Program
    {
        enum MyEnum          // enum
        {
            Red, Green, Blue
        };

        static void Main()
        {
            MyEnum r = MyEnum.Red;
            Console.WriteLine(r);   // prints Red

            MyEnum g = MyEnum.Green;
            Console.WriteLine(g);   // prints Green

            MyEnum b = MyEnum.Blue;
        } 
    }
}
In the above code, we have an enum MyEnum with three parameters Red, Green and Blue. In the Main() method, we are creating instances to MyEnum
This will print the following lines in your console,


Now, to see what is there in the debugger, put a breakpoint and press F5. This will automatically opens the debugger window in which we will see the following items,

Please note that, the debugger is showing r, g and b are of type Example.Program.MyEnum. But these variables are stored as integers (internally).

We know that we cannot assign enums to any value as they are strongly-typed.

Nested types

We have already seen the introduction of nested types in C#. Let's see the nested types with enums and other types.

syntax of a nested type is,
class Main           // Main class
{
    class Nested     // Nested class
    {
        ...............;
	.............;
    }
}
Nested types are the types which are declared within the scope of another type such as we declared Nested within the scope of Main.

Now let's take an example of nested types with enums,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Example
{
    class Program
    {
        public class TopClass
        {
            public class NestedClass { }                    // nested class NestedClass.....
            public enum Standard { First, Second, Third }   // nested enum Standard......
        }

        static void Main()
        {
            TopClass.Standard Itm1 = TopClass.Standard.Second;
            Console.WriteLine(Itm1);                        // prints Second

            TopClass.Standard Itm2 = TopClass.Standard.Third;
            Console.WriteLine(Itm2);                        // prints Third
        } 
    }
}
In the above example, we have a class NestedClass and enum Standard nested within the main class TopClass.
  • These nested classes can access the private members of the enclosing type (i.e. TopClass).
  • They can access all the types that are accessed by the enclosing type.
  • We can declare them with any kind of access modifiers.
  • Default access modifier is private.
  • In order to access these types from outside the main enclosing type (i.e. TopClass), We have to provide the name of the enclosing type (i.e. TopClass.Standard Itm1 = TopClass.Standard.Second; in our example).
The above code prints the following lines in your console,


Any types such as classes, structs, enums, interfaces and delegates can be allowed to be nested inside a class or a struct.

Accessing static members:
We can access static members of a enclosing type (i.e class or struct) from a nested type like below,
public class MyClass                 // Enclosing type
{
    static int i = 5;
    class MyNested                   // Nested type
    {
        static void Test()
        {
            Console.WriteLine(MyClass.i);  // Accessing static member
        }
    }
}
In the above code, we have a class MyClass with a nested class MyNested. In the Test() function of MyNested, we are accessing the static member 'i' (of MyClass) by using the name of the enclosing type (MyClass.i). 

Accessing protected members:



Now let's see an example of applying the protected modifier to the nested type and accessing it from the derived class members,
public class BaseClass                 // Base class
{
    protected class MyNested           // Nested class
    {
        void Test()
        {
            Console.WriteLine("This is a Test() method of MyNested class");
        }
    }
}

public class DerivedClass : BaseClass  // Derived class
{
    static void Func()                 
    {
        new BaseClass.MyNested();      // Accessing Nested class by using the name of the type BaseClass
    }
}
In the above example, we have a base class (BaseClass) with a nested class MyNested and a derived class (DerivedClass).
We are accessing the nested type MyNested from the derived class by using the BaseClass name (i.e. BaseClass.MyNested).

Accessing nested type from outside:
We can also access a nested type from outside the class (main class) that we already seen in the above example. Now let's take another example,
public class MyClass        // Main class .... enclosing type
{
    public class MyNested   // Nested class
    {
        public void Test() { }
    }
}

static void Main()
{
    MyClass.MyNested my = new MyClass.MyNested();  // Accessing the nested class
}
In the above example, we have a nested class MyNested in the main enclosing type MyClass. we are accessing the nested type from outside the class that is from the Main method of the program. This can be done by using the name of the enclosing type (i.e. MyClass.MyNested).

Conclusion

In this article, we have seen debugging enums and nested types of C#. Hope you understand.

Thanks for reading,

Regards,
Krishna.

Recommendation
Read Generics and generic types in C# after this article.
Page copy protected against web site content infringement by Copyscape

About the Author

Goud.Kv
Full Name: Krishna Vamshi Goud
Member Level: Gold
Member Status: Member,MVP
Member Since: 2/12/2014 2:34:09 AM
Country: India
Thanks & Regards, Krishna


Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)