C# Class, Interface, Enum and Other Concepts in UML

You can model your C# software in UML. The classes can be modeled using UML class diagrams. It is possible to depict abstract, static, generic classes and their members.

C# Class in UML

A simple class can include contructors, fields and methods. The visibility of fields and methods can be restricted to:

  • private - is accessible only within the same class, UML symbol is minus -
  • protected - is accessible within the same class and inherited classes, UML symbol is hash #
  • internal - is accessible within the same assembly, UML symbol is tilde ~
  • public - is freely accessible, UML symbol is plus +

Read-only fields (C# keyword: readonly) are marked with a UML {readonly} modifier. To set it, right-click on a UML attribute and choose Modifiers from the context menu and then Read-Only.

Static fields (C# keyword: static) are expressed using a UML static modifier. They are depicted as underlined text. To set it, right-click on a UML attribute and choose Modifiers from the context menu and then Static.

C# Class in UML
C# Class in UML

C# Properties in UML

C# properties can be modeled as UML attributes with property stereotype. You can set the stereotype in multiple ways:

  • type <<property>> before the attribute name when editing an attribute in the diagram editor
  • select an attribute and choose from the ribbon - Field tab / Add group / Stereotype button / property
  • right-click on an attribute and in the context menu choose Stereotypes / property

Software Ideas Modeler allows you to define source codes for the get and set body of a property.

C# Abstract Class in UML

An abstract class with abstract properties and methods can be modeled using UML notation. An abstract class cannot be instantiated and it can contain methods without implementation. The logic of method bodies can be defined in subclasses. An abstract field and abstract method are rendered using an italic font.

C# Abstract class in UML
C# Abstract class in UML

C# Static Class in UML

A static class with static fields and methods can be modeled using UML notation. A static class cannot be instantiated or inherited and it can contain only static members. A static class represents a singleton design pattern. A static class is defined using static stereotype in UML.

C# Static class in UML
C# Static class in UML

C# Generic Class

A generic class defines generic parameters. They are represented by UML template parameters.

You can add a new template parameter to a class using the ribbon / Model tab / Add group / Template Parameter button.

C# Generic class in UML
C# Generic class in UML

C# Events and Delegates

You can also model events and delegates using UML tool Software Ideas Modeler. Events are modeled as UML attributes with an event stereotype. Delegates are modeled as UML operations with a delegate stereotype.

UML for C# Event
UML for C# Event

C# Attributes

UML does not provide direct support for the concept of C# attributes. However, you can use tagged values. You can add tag Annotation with an attribute definition in the value using Software Ideas Modeler to add C# attribute to a class, field, or method. The source generator will translate the tagged value to the attribute.

C# Class with attributes in UML
C# Class with attributes in UML

C# Inheritance in UML

An inheritance of classes can be modeled directly using UML. You can connect a subclass with a superclass using UML generalization. The triangle head arrow of generalization points from a subclass to a superclass.

C# Class inheritance
C# Class inheritance

C# Nested Classes in UML

A nested class can be defined using UML containment relationship. The connector is depicted as a circle with the plus sign inside, the circle end is connected to the container class, the other end is connected to the nested class.

C# Nested classes in UML
C# Nested classes in UML

C# Class Composition in UML

You can use a class as a field type of another class using the UML composition association. In this way, classes can create complex hierarchies.

C# Class composition in UML
C# Class composition in UML

C# Interfaces in UML

Interfaces define what members are expected from a classifier, but it does not define any implementation for them. C# interfaces can be modeled using UML interfaces. UML interfaces are associated to classes using realization relationships.

C# Interfaces in UML
C# Interfaces in UML

C# Enum in UML

An enumeration defines a set of named values. Numeric values can be defined for particular enumeration items. C# enums are modeled using UML enumerations. If you want to define an enum with flags - use flags stereotype for the enumeration.

C# Enumeration in UML
C# Enumeration in UML

C# Namespace in UML

A namespace is defined using UML package element. The root namespace can be defined in the repository element (diagram, folder). The setting is available as Custom Namespace field in the Properties dialog for Diagram or Folder. The field must be checked and filled.

C# Class inside a namespace in UML
C# Class inside a namespace in UML

C# Using in UML

Usings can be defined for the particular classes using tagged values. Enter the desired usings as values for Import tags

Comments

nudelchef 11 March 2021 9:34:11

C# Properties in UML

How can one define a public property on a protected field?

Dusan Rodina - softwareideas.net 12 March 2021 9:19:45

RE: C# Properties in UML

You can add two attributes to the UML class - one protected for field and one public for the property. The property attribute will be annotated with «property» stereotype.

Cool 9 February 2022 14:22:34

Suggestions

Very cool and clean article. Unless I did not find the way, it would be an asset to have snippets showcasing how these concept translate to C#.

I never saw the Nested Class symbol. I learnt something, but Aggregation is missing. I believe it is more likely to be used.

Dusan Rodina - softwareideas.net 9 February 2022 21:48:22

RE: Suggestions

Thank you very much for your feedback. Your suggestions will be incorporated into the article.

New Comment

Comment