Code Design and Visualization with Visual C# 2005 (
Page 1 of 2 )
The class diagrams built into Visual C# 2005 let you visualize code, so you can more easily understand the relationships in your programs. Find out how they work, and what they can do for you.
One of the exciting new features in Visual C# 2005 is Class Diagrams. Class diagrams give developers IDE support for visualizing the static structure of code. Furthermore, the class diagram capability enables visual design of new code. This article discusses both of these exciting new features, and shows you the details of making them work for you.
Why Visualization?
ADVERTISEMENT
To understand the need for code visualization, think of how many times you've begun maintenance work on a piece of code that someone else wrote. Similarly, ponder the times you've returned to your own code, after more than a week of not working on it. In most cases, it takes time to acclimate yourself to how the pieces fit together.
The Class Diagram helps in this process, by allowing you to visualize the structure of an application with a graphical representation of your code. It displays types, their members, and associations between them. The result is that you now have an opportunity to get up-to-speed in understanding the makeup and relationships of objects in your application quicker than reading code alone.
The Initial Class Diagram
The process of visualization assumes you have some code that has already been written. It could have come from someone else, or could potentially be code that you wrote yourself. To get started, the code must be a part of a Visual C# project. To pull the code in from somewhere else, open the project you want to work with, right-click on the project in Solution Explorer, and select Add/Existing Item. Listing 1 shows some code I wrote to demonstrate visualization of various types and their relationships.
Listing 1. The purpose of this listing is to demonstrate the visualization process and how each type appears in the Class Diagram.
using System;
namespace Visualization
{
class Program
{
private MyDerivedClass m_derived = new MyDerivedClass();
public MyDerivedClass MyDerivedClass
{
get { throw new NotSupportedException(); }
set { }
}
static void Main(string[] args)
{
}
}
public interface IMyInterface
{
void MyRequiredMethod();
int MyRequiredProperty { get; set; }
}
public class MyBaseClass
{
public void MyRequiredMethod() { }
private int[] m_myNums = { 1, 2, 3 };
protected int this[int index]
{
get { return m_myNums[index]; }
set { m_myNums[index] = value; }
}
protected internal void MyBaseMethod() { }
}
public sealed class MyDerivedClass : MyBaseClass, IMyInterface
{
private int m_myInt;
public int MyRequiredProperty
{
get { return m_myInt; }
set { m_myInt = value; }
}
public void MyDerivedMethod()
{
}
public event MyDelegate MyEvent;
}
public class MyClass : IMyInterface
{
private int m_myInt;
public int MyRequiredProperty
{
get { return m_myInt; }
set { m_myInt = value; }
}
public void MyRequiredMethod()
{
throw new Exception("The method or operation is not implemented.");
}
public event MyDelegate MyEvent;
}
public struct MyStruct : IMyInterface
{
private int m_myInt;
public int MyRequiredProperty
{
get { return m_myInt; }
set { m_myInt = value; }
}
public void MyRequiredMethod()
{
throw new Exception("The method or operation is not implemented.");
}
public event MyDelegate MyEvent;
}
public delegate void MyDelegate();
}
To visualize the code in Listing 1, right-click on its containing project in Solution Explorer and select View Class Diagram. This produces the image shown in Figure 1.