How to set interface implementation in generic interface? [duplicate] - c#

This question already has answers here:
How can I use interface as a C# generic type constraint?
(11 answers)
Closed 3 years ago.
I would like to write generic interface
interface IFoo<TYPE>{}
But also I would like to restrict this TYPE like this (for example)
interface IFoo<TYPE: MyAnotherClass>{}
So, it is means that I don't want that user will pass any TYPE that don't implement MyAnotherClass
How to achieve this behavior in C#?
P.S. In Java(for example) it is possible...

interface IFoo<T> where T : MyAnotherClass {
}

Related

In C# when using generics, and using the constraint where T : new() can the new() operator ever have a parameter? [duplicate]

This question already has answers here:
Is there a generic constructor with parameter constraint in C#?
(9 answers)
Closed 1 year ago.
I am trying to do something like this:
where DataTableLoader2 is a generic helper class
public static class DataTableLoader2 <T> where T : class, new (StringComparer)
Thanks for any help offered....
K
No, it can't. The purpose of new() is simply to enforce that T must have a default, parameterless constructor. If you want to enforce a generic having some common initialization then you should do it through either a base class, or an interface.

What is this C# generic constraint does: public class ValueObject<T> where T : ValueObject<T> [duplicate]

This question already has answers here:
c# generic self-referencing declarations
(4 answers)
Closed 2 years ago.
I don't understand this generic constraint where the constraint is the same type as the class.
public class ValueObject<T> where T : ValueObject<T>
What does this constraints? When it is used?
Generic T must inherit of gived type.
where T : BaseClass.

Implement an interface while calling a method (inline) [duplicate]

This question already has answers here:
Can anonymous class implement interface?
(9 answers)
Closed 6 years ago.
In Java, assuming that class A is an interface/abstract class, I can do the following:
callMethod(new A(){
public void myFunc(){
System.out.println("test");
}
});
How can I achieve the same shortcut effect in C# without having to declare a class seperately .
Thanks
You can't.
C# does not allow you to implement interfaces on Anonymous Types.

What's the correct way to read ISomething : ISomethingElse [duplicate]

This question already has answers here:
What do you call it when one interface "inherits" from another?
(6 answers)
Closed 6 years ago.
Please forgive me if this is a very dumb question but I'm looking for clarification on how to interpret the following into plain English (assuming ISomethingElse is an existing interface):
public interface ISomething : ISomethingElse {
}
Would I read that as:
"Public interface ISomething INHERITS interface ISomethingElse"
or
"Public interface ISomething IMPLEMENTS interface ISomethingElse"
I'm leaning towards "inherits" because I've always understood an interface to be a "contract" for something, not an "implementation" but I'm just not sure what's the proper way.
I'm concerned primarily with C# (if that makes a difference).
The C# 5.0 specification uses "inherits". From ยง13, "Interfaces":
An interface defines a contract. A class or struct that implements an interface must adhere to its contract. An interface may inherit from multiple base interfaces, and a class or struct may implement multiple interfaces.
(Emphasis added.)

Is it necessary to explicitly inherit from object? [duplicate]

This question already has answers here:
Redundant to inherit from Object in C#?
(7 answers)
Closed 7 years ago.
In this MSDN example, the class explicitly inherits from Object:
class Point: Object {
// ...
}
Is explicitly inheriting from Object ever necessary? That is, is it not equivalent to the following?
class Point: Object {
// ...
}
No, it is implicitly inherited. This is why every class in C# has a .ToString() and .Equals().
https://msdn.microsoft.com/en-us/library/vstudio/system.object%28v=vs.100%29.aspx
Object class: "This is the ultimate base class of all classes in the .NET Framework; it is the root of the type hierarchy."
This type of inheritance where everything is derived from a single class is called a "unified type system"

Categories

Resources