'How to deal with objects of certain types c#
I have a total of 5 classes to which I always create an object. From this collection of objects a list is created with different types of objects in the list.
List<object> objectsEntries = new List<object>();
All these objecte have properties that I would like to use.
However, this turns out to be a bit confusing. Because for each entry in the list I first have to check which type it is. Then I have to cast the object to be able to use it.
Like this:
public void DoSomething(object objectsEntry)
{
if (objectsEntry is Class1)
{
Class1 commandTyped = (Class1)objectsEntry;
// Now I can use the properties of Class1
// e.g.
// Console.WriteLine(commandTyped.Name);
}
else if (objectsEntry is Class2)
{
Class2 commandTyped = (Class2)objectsEntry;
// Now I can use the properties of Class2
// e.g.
// Console.WriteLine(commandTyped.Name);
}
}
But the whole method becomes very confusing and no matter where I want to use the objecte it comes to problems, because I always have to determine the type before and then cast it.
Additionally all classes have completely different properties.
Are there any possible solutions or approaches to this?
Solution 1:[1]
I would like to do this in this way:
public interface ISomeClasses
{
int Id { get; set; }
}
public class SomeClass1 : ISomeClasses
{
public int Id { get; set; }
}
public class SomeClass2 : ISomeClasses
{
public int Id { get; set; }
}
public class Main
{
public void DoSomething(object objectsEntry)
{
if (objectsEntry is ISomeClasses someClass)
{
Console.WriteLine(someClass.Id);
}
}
}
Short explain:
- you create an interface with property which you're interesting in
- create few classes which all implements the same interface
- and in yours method called "DoSomething" you could check if object what you have implement the interface and if yes you assign it to variable called "someClass" and you have there access to property which you're interesting in
Solution 2:[2]
If you create an interface containing the method DoSomething(), you can inherit from that interface in all your custom classes and implement the desired method body of DoSomething() for each, e.g.:
public interface ISomething
{
void DoSomething();
}
public class Class1 : ISomething
{
public string Name { get; set; }
public void DoSomething()
{
Console.WriteLine($"[{nameof(Class1)}] My name is {Name}");
}
}
public class Class2 : ISomething
{
public int Age { get; set; }
public void DoSomething()
{
Console.WriteLine($"[{nameof(Class2)}] I am {Age} years old");
}
}
// Other Class* implementations
You can then create a list of your interface type and populate it with objects of your custom classes. Calling DoSomething() on each entry will then call the specified implementation for the appropriate Class* type.
List<ISomething> entries = new()
{
new Class1 { Name = "Sarah" },
new Class2 { Age = 41 }
};
foreach (var entry in entries)
{
entry.DoSomething();
}
produces:
[Class1] My name is Sarah
[Class2] I am 41 years old
Example fiddle here.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | LukasCwaj |
| Solution 2 |
