Question about Interface

Aurel

Member
Joined
Sep 13, 2022
Messages
6
Programming Experience
Beginner
Hi there,

First things first, I'm using C# Core 7 preview not C#6 (not available in the list but it's whatever).

So I create a basic interface :

C#:
public interface IRegularPolygon
{
    static readonly int SideCount;

    public RectangleF Bounds { get; }

    public IList<PointF> Points { get; }
    
    public static abstract IRegularPolygon Create(Point location, int radius);

    public static abstract Point GetCentroid(PointF[] points);
}

and I have classes impenting this. Example :

C#:
class Hexagon : IRegularPolygon
{ 
// More codelines


     public static IRegularPolygon Create(Point location, int radius)
    {
        // Creating the Hexagon
    }
    
}

My question is, why can't I make a method public static Hexagon Create(Point location, int radius) ? since it's returns Hexagon, which implements IRegularPolygon ?

Thanks for your answers in advance !

Aurélien.
 
Please provide ALL the relevant information. That includes what you're trying to accomplish, how you're trying to accomplish it and what happens when you try. Two outta three ain't bad, but it ain't good enough. If thew system gives you an error message, you give it to us.
 
Sorry the error is in case 3 in the code below

Elsewhere in my program I call : var hexagon = (Hexagon)Hexagon.Create(location, radius).

That is why I asked if I could do this :


C#:
class Hexagon : IRegularPolygon
{
    // Working
    public static IRegularPolygon Create(Point location, int radius)
    {
        // Creating the Hexagon
    }
 
    // Working too, but I would like to avoid casting
    public static IRegularPolygon Create(Point location, int radius)
    {
        // Creating the Hexagon
        return (Hexagon)hexagon;  // still need to cast (Hexagon) when calling this method
    }
 
   // What I want to do
  // [ERROR] Don't implement IRegularPolygon
  public static Hexagon Create(Point location, int radius)
  {
      // Creating the Hexagon;
      return hexagon // as Hexagon type
  }

}

I honnestly have no idea if this is achievable. Hence my question.

So why can't I have a method that returns Hexagon since it's a IRegularPolygon ? Or maybe I inherit from a class instead ? (but I prefer interface, if possible)
 
Last edited:
This seems to compile for me without requiring any casting...
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Versioning;
using System.Text;

var hex1 = Hexagon.Create();


[RequiresPreviewFeatures]
interface IShape
{
    static abstract IShape Create();
}

[RequiresPreviewFeatures]
class Hexagon : IShape
{
    // Make constructor private to force using the factory method
    private Hexagon()
    {
    }

    public static Hexagon Create()
        => new Hexagon();

    static IShape IShape.Create()
        => new Hexagon();
}
 
Or if you really don't like the duplication...
C#:
[RequiresPreviewFeatures]
interface IShape<T> where T : IShape<T>
{
    static abstract T Create();
}

[RequiresPreviewFeatures]
class Hexagon : IShape<Hexagon>
{
    // Make constructor private to force using the factory method
    private Hexagon()
    {
    }

    public static Hexagon Create()
        => new Hexagon();
}
 
Your second example with a T is working like a charm. I gave it a try this morning but it seems like I made some mistakes !

Thanks you very much ! Problem solved and it really looks nice and neat to me !

Edit : I had to put in the .csprof file this to suppress warnings :
<PropertyGroup>
<EnablePreviewFeatures>true</EnablePreviewFeatures>
</PropertyGroup>
 
Last edited:
One more question. I'm getting the CA1000 warning "do not declare static members on generic types", should I ignore it or is there a workaround ?
 
Last edited:
Last edited:
See The extra text I added above.
 
Yes, I clicked this link on Visual Studio and read this article.

As you probably assumed, I'm nothing but a beginer. In my option "it's ok" but who am I to say so...

Thank you very much for your time and sharing your knowledge Skydiver !
 

Latest posts

Back
Top Bottom