Introducing Dragonfly Cloud! Learn More

Error: Unity Warning CS0108

What's Causing This Error

The 'CS0108' is a Compiler Warning typically seen in Unity. It surfaces when a member in a class or struct is declared with the same name as another member inherited from a base class. In other words, you have a derived class that hides an inherited member from the base class without using the new keyword, causing ambiguity. The warning is simply letting you know that there could be some confusion later if you're expecting to access the base member but are actually accessing the derived member.

For instance, if a base class 'A' has a function named 'foo()', and you have a derived class 'B' which also has a function named 'foo()', this warning will pop up because 'foo()' in 'B' is hiding 'foo()' in 'A'.

Solution - Here's How To Resolve It

There are a couple of strategies that you can use to resolve this warning:

  1. Use the new keyword: This keyword is used to hide an inherited member and avoid any potential ambiguity. By doing so, you're explicitly telling the C# compiler that you intend to hide the base class method. If we take the example mentioned above, in class 'B' you would define your method like this: new void foo() { ... }.

  2. Rename the member: Another straightforward approach is to rename one of the members. This solution is especially useful when you don't actually need to override the base class method or when the derived class method performs a completely different operation than the base method.

  3. Implement overriding properly: If your intention was to override the functionality of the base class method in the derived class, use the override keyword instead of just naming a method with the same name. The base class method must be marked as virtual, abstract, or override.

It's important to choose the right solution based on your use case and programming intention. Sometimes, hiding a base class member is necessary, but often renaming or properly overriding the method can lead to cleaner, less confusing code.

Was this content helpful?

Start building today 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.