-1

With C# generics (specifically, type parameters for classes), is it possible to refer to specified type parameter values in an specialized ("non-generic") type (class) that extends/inherits the generic one? In this case, I'm overriding virtual methods of the generic (inherited) type in the inheriting (specialized) one; I suspect that's important to the question/answer, but don't want to limit the case if not.

I might just be looking for what C calls a typedef; the closest thing I can think of in C# is a using alias (using Type = Some.Longer.Namespaced.Type;). Here is an example w/ the using alias, but even this is still very verbose and is no fun to update when e.g. the name of ConcreteType changes (or some other refactor, where the boundaries/encapsulations of logic shift, not the logic itself):

using ConcreteType = Some.Longer.Namespaced.Type;

public class ConcreteTypeLogic : CrudLogic<ConcreteType>
{
    // default ctor for `ConcreteTypeLogic`
    public ConcreteTypeLogic()
    { /* ... */ }
    // other ctor for `ConcreteTypeLogic`
    /* ... */
    // dtor for `ConcreteTypeLogic`
    public ~ConcreteTypeLogic()
    { /* ... */ }

    // "Create" implementation for `ConcreteType`
    public override ConcreteType Create(ConcreteType value)
    { /* ... */ }

    // "Read" implementation for `ConcreteType`
    public override ConcreteType Read(ConcreteTypeIdentifier valueId)
    { /* ... */ }

    /* ... */
}

It seems like a C-style typedef or a CPP-style macro is really what I want though, because ConcreteTypeLogic and the constructor/destructor will need to update as well, as well as any other relevant aspects where the type (class) name is part of the binding/contract.

Nathan Schulte
  • 125
  • 2
  • 12
  • The `using` method you've demonstrated is the only way to do what you're trying to do. – itsme86 Oct 14 '16 at 22:50
  • 1
    I don't really see the dilemma though. Unless you're using notepad, the IDE you're using should be able to rename all instances of whatever type `ConcreteType` aliases for you when you rename the type. – itsme86 Oct 14 '16 at 22:56
  • 1
    _" but even this is still very verbose and is no fun to update when e.g. the name of ConcreteType changes"_ -- what do you mean? If the actual type name changes, you need to change it only in the alias declaration. But that should be automatic using normal refactoring/renaming features in your IDE. If you want to change the alias name itself, again the refactoring/renaming features should work fine. What's so hard? What is it you're trying to avoid that you think could be avoided and which isn't now using a type name alias? – Peter Duniho Oct 14 '16 at 23:01
  • Ah, okay, I understand then: the binding sites for `ConcreteTypeLogic` will need to update w/ the new name as well, and that list is unbounded (by design), and hence the issue. What I'm really looking for, then, is a better syntax for specifying c/dtors, and other "aspects" that have the same "names must match, but compiler can infer the name..." issues; ctor/dtor is the only one that comes to my mind. Why are these responses comments, and not answers? – Nathan Schulte Oct 14 '16 at 23:50
  • I think I understand why the ctor/dtor has to bind a name too: inheritance. Possibly also, `partial` needs this binding in order to work. Still, though, a sugar-syntax for "the immediately containing type declaration's name" would be handy. I don't think type extensions allow specifying ctor/dtor, but perhaps there's a concern there, too. Does something like this exist? – Nathan Schulte Oct 14 '16 at 23:57
  • Just to be clear, the verbosity you're talking about refers to having to spell out a type's name for its ctors and dtor (3x `ConcreteTypeLogic`, instead of once), and having to repeat generic type parameter names (the `ConcreteType` argument and return types in your example)? – Pieter Witvoet Oct 15 '16 at 23:43
  • @PieterWitvoet, yes, I think that clarifies. The "having to repeat generic type parameter names" bit, I think the `using` alias I demonstrated solves (at least, if you e.g. ignore `partial` and assume all of the `*Logic` impl is in the one src file). The ctor/dtor, and any other facet of the lang that has the same requirement (to match the class name), is the last bit of verbosity I'd like to minimize. – Nathan Schulte Oct 16 '16 at 17:40
  • I see what you mean, but in practice I find IDE features like auto-completion, code templates and refactor operations to handle this sort of verbosity very well. I don't think there's a way to work around C#'s constructor syntax anyway. As for destructors (finalizers), those are relatively rare in C#. – Pieter Witvoet Oct 16 '16 at 19:25
  • PeterDuniho, @PieterWitvoet, you should make your comments answers, because they're the right answer to my naive question. – Nathan Schulte Oct 18 '16 at 20:35

1 Answers1

0

Outside of the using alias, the language (C#) doesn't support this naming indirection.

It's not common to use a pre-processor when building C# (.NET) projects, and the common alternative is to use development tools to expedite this "work." Considering that the definition of a type doesn't need to reside in a single source file, or even a single assembly, development tooling seems the best approach to managing this.

Thanks to the commenters for their responses.

Nathan Schulte
  • 125
  • 2
  • 12