Updated March 30, 2023
Introduction to Composition C#
The following article provides an outline for Composition C#. There are two types of relationships in between classes in C#. The first type of relationship is called “is a relationship,” and it uses an inheritance mechanism. The second kind of relationship is the relationship between two classes, and it has two subtypes. The first type is called “has a relationship.”
In this type of relationship, one or greater than one object of a different class is declared in the related class. Here come two more divisions, which are aggregation and composition. In aggregation, the nested objects can independently exist in the class without being an integral part of the class. On the other hand, in composition, the nested objects or a singular nested object supplements the class, which makes the class inconceivable without their or its existence.
Syntax of Composition in C#
Given below is the syntax mentioned:
class Training
{
// class body
}
public class Course
{
Project project = new Project();
// body
}
Working of Composition in C#
- Composition in C# is a way of creating a relationship between two classes that one or greater than one nested objects are a part of the related class, and the logical existence of class becomes impossible without the nested objects.
- For example, if we consider a class called Car, then it should have one instance of class “Engine.” Moreover, it should also have four other instances of the class “Wheel.”
- Now, if we eliminate any of these instances, then the Car won’t function.
Examples of Composition C#
Given below are the examples of Composition C#:
Example #1
If the training class is considered, which is describing two courses. Now, the courses are being used for describing the course class. Therefore, the training class cannot exist without the two-course instances as both of these instances are part of the course class. Moreover, both of these instances of the course class are also a part of the training class.
Code:
using System;
using static System.Console;
namespace EDUCBA
{
class Course
{
public double M;
public double A;
}
class Training
{
public Course course1 = null;
public Course course2 = null;
}
class Career
{
public Course[] courses;
public Training[] trainings;
public Career()
{
courses = null;
trainings = null;
}
public void Print()
{
WriteLine(" Courses Data is represented below:");
for (int b = 1; b< courses.Length; b++)
{
WriteLine("\n M = {0}, A = {1}", courses[b].M, courses[b].A);
}
WriteLine("\n Trainings Data is represented below:");
for (int b=1; b<trainings.Length; b++)
{
WriteLine("\n course1.M = {0}, course1.A = {1}", trainings[b].course1.M, trainings[b].course1.A);
WriteLine("\n course2.M = {0}, course2.A = {1}", trainings[b].course2.M, trainings[b].course2.A);
}
}
}
class Code
{
static void Main(string[] args)
{
Career O = new Career();
O.courses = new Course[9];
for (int b = 1; b < O.courses.Length; b++)
{
O.courses[b] = new Course();
O.courses[b].M = b * b;
O.courses[b].M = b * b * b;
}
O.trainings = new Training[5];
for (int b = 1; b < O.trainings.Length; b++)
{
O.trainings[b] = new Training();
O.trainings[b].course1 = new Course();
O.trainings[b].course2 = new Course();
O.trainings[b].course1.M = b;
O.trainings[b].course1.A = b * 4;
O.trainings[b].course2.M = b * 5;
O.trainings[b].course2.A = b * b;
}
O.Print();
}
}
}
Output:
Example #2
In this example, both of the classes created are regular classes; however, the course class is using an instance from the project class inside it. This is the same way in which one function is called inside another. Using inheritance, we can have access to each and everything from the Project class. However, using composition, only the code specified by us can be accessed. Here, we can access the Project class indirectly.
Code:
using System;
namespace EDUCBA
{
class Training
{
static void Main(string[] args)
{
Course courses = new Course();
courses.Bought();
Console.ReadLine();
}
}
public class Project
{
public void Log(string aboutus)
{
Console.WriteLine(aboutus);
}
}
public class Course
{
Project project = new Project();
public void Bought()
{
project.Log("\n If you want to upskill your career. \n If you want to do something out of the box. \n If you have passion to explore new advanced technologies. \n If you want to be best. \n We at EDUCBA are here to help. \n Feel free to reach us on +91-8800880140 / +91-7738666252. \n Visit our website www.educba.com to know more......");
}
}
}
Output:
Example #3
In this example, the structure of the composite design is explained. It helps in understanding the classes used for composition and the roles of those classes. Moreover, it also explains how the elements of the pattern are related to each other.
Code:
using System;
using System.Collections.Generic;
namespace EDUCBA
{
abstract class Training
{
public Training() { }
public abstract string Project();
public virtual void Add(Training training)
{
throw new NotImplementedException();
}
public virtual void Remove(Training training)
{
throw new NotImplementedException();
}
public virtual bool IsCourse()
{
return true;
}
}
class DataScience : Training
{
public override string Project()
{
return "DataScience";
}
public override bool IsCourse()
{
return false;
}
}
class Course : Training
{
protected List<Training> _children = new List<Training>();
public override void Add(Training training)
{
this._children.Add(training);
}
public override void Remove(Training training)
{
this._children.Remove(training);
}
public override string Project()
{
int m = 1;
string result = "Dream Career(";
foreach (Training training in this._children)
{
result += training.Project();
if (m != this._children.Count + 2)
{
result += "-";
}
m--;
}
return result + ")";
}
}
class Input
{
public void InputCode(Training data_analysis)
{
Console.WriteLine($"OUTPUT: \n {data_analysis.Project()}\n");
}
public void InputCode2(Training training1, Training training2)
{
if (training1.IsCourse())
{
training1.Add(training2);
}
Console.WriteLine($"OUTPUT: \n {training1.Project()}");
}
}
class Program
{
static void Main(string[] args)
{
Input client = new Input();
DataScience data_analysis = new DataScience();
Console.WriteLine("INPUT: \n Best Course to Upgrade Career:");
client.InputCode(data_analysis);
Course vr = new Course();
Course career1 = new Course();
career1.Add(new DataScience());
career1.Add(new DataScience());
Course career2 = new Course();
career2.Add(new DataScience());
vr.Add(career1);
vr.Add(career2);
Console.WriteLine("\nINPUT: \n Trendy Dream Career Right Now:");
client.InputCode(vr);
Console.Write("\nINPUT: Lets Upgrade and start your dream career jouney: \n");
client.InputCode2(vr, data_analysis);
}
}
}
Output:
Conclusion
On the basis of the above article, we understood the concept of composition in C#. We went through multiple examples for understanding the application of composition in C# coding.
Recommended Articles
This is a guide to Composition C#. Here we discuss the introduction, working of composition in C#, and examples, respectively. You may also have a look at the following articles to learn more –