Type Name aliasing in C# [duplicate] - c#

This question already has answers here:
Closed 13 years ago.
Possible Duplicates:
typedef in C#?
STL like containter typedef shortcut?
I was wondering if there was an equivalent to Type aliasing from Functional Languages that I can use in C#
For example in a nice functional language like Haskell I can say something like the following to alias an existing type to a custom Type Name
type MyCustomTypeName = String
I'd like to do this as I have an API that I'm building where some of the Objects I'm using have multiple possible names in the sense they could be referred to by several terms which are interchangeable and equivalent. Presumably I could do this with inheritance but that seems somewhat clunky and then potentially breaks if people start extending the non-canonical class ie.
public class CanonicalClass {
//Full Implementation
}
public class AlternateName : CanonicalClass {
//Empty except I'll need to redefine all the constructors
//Could declare it sealed but doesn't get rid of the need to redefine constructors
}
And before anyone mentions interfaces all the Classes in question are all implementing interfaces already and there are multiple differing implementations of these interfaces.

Depending on what you're actually trying to do (give a somewhat more complete example), you may indeed need interfaces (used properly) and/or generics.

Related

Why POCO objects are created using classes instead of structs [duplicate]

This question already has answers here:
When should I use a struct rather than a class in C#?
(31 answers)
Closed 4 years ago.
Why POCO objects are created using classes instead of structs in C#, while POCO is intended to carry only public attributes and not any behaviors?
Do we really need to create a class with get and set accessors? I think structs would be much cleaner and simple to carry object's state.
In C#, struct type is a value, so looks that it is not a good choice.

Why does the .NET framework rely on interfaces? [duplicate]

This question already has answers here:
Why would I want to use Interfaces? [closed]
(19 answers)
Closed 8 years ago.
I am working on learning C# in depth. I am mostly confused by the frequent implementation of interfaces. I always read that this class implements this interface. For instance, SqlConnection class implements IDbConnection. What is the benefit for developers in this case?
the interfacing is based on object-oriented principles, e.g. see SOLID. You should not rely on implementation of other classes you're working with - it should be sufficient for you to know only what they do and what they should return. A good example with the SqlConnection would be that you may be able to change the DB you are using quite simply (to e.g. MySQL or Oracle) by changing the implementation on just one place, providing that your code is correctly using the interfaces and propagating the instances.
An interface contains definitions for a group of related functionalities that a given type must implement (a sort of Method Signature Contract). It does not, however, guarantee the specific behavior of those implementations.
Interfaces are particularily useful as they allow the programmer to include behavior from multiple sources in programming languages that do not support multiple inheritance of classes like C#.

What is the java equivalent bounded wildcard in C# properties? [duplicate]

This question already has answers here:
C#'s equivalent of Java's <? extends Base> in generics
(4 answers)
Closed 9 years ago.
I’m looking for equivalent C# code of following line
private List<? extends HotSpot> hotSpots;
Any help is much appreciated.
Depending on exactly what you need, you're probably either looking for:
public class MyClass
{
private IList<HotSpot> hotSpots;
}
or using where
public class MyClass<T> where T : HotSpot
{
private IList<T> hotSpots;
}
Functionally, I'd say the closest is:
IEnumerable<HotSpot> hotSpots;
If the real type of the the enumerable happens to be an IList, then ElementAt() and such will be O(1).
As of .NET 4.5, you could also use:
IReadOnlyList<HotSpot> hotSpots;
and use List.AsReadOnly() to wrap regular lists.
The .NET approach to variance in generics is letting specific interfaces be either covariant or contravariant, and consequently only allow them to define methods with the generic type parameter as the return value only, or only in the argument list. (As opposed to Java where the compiler does these checks in each expression.) My guess is the rationale is that C# implements generics using reification, and a concrete type like List<out T> can't exist in the type system.

System.Enum as a generic type parameter with constraints [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Create Generic method constraining T to an Enum
Enum type constraints in C#
Consider the following class:
public class Transition<TState>
{
public Transition ()
{
if (!typeof(TState).IsEnum)
throw (new ArgumentException("[TState] has to be of type [System.Enum]."));
}
}
Ideally, this should be declared as:
public class Transition<TState> where TState: System.Enum
{
}
The above, of course, generates a compile-time error. My question is why has that been made illegal. Most sources explain say that it is illegal but do not explain why. Any thoughts?
As Eric Lippert says that and I quote
ALL features are unimplemented until someone designs, specs, implements, tests, documents and ships the feature. So far, no one has done that for this one. There's no particularly unusual reason why not; we have lots of other things to do, limited budgets, and this one has never made it past the "wouldn't this be nice?" discussion in the language design team."

implementing a custom cast for two types in c# [duplicate]

This question already has answers here:
C# adding implict conversions to existing types
(2 answers)
Closed 9 years ago.
I have two custom classes for which I want to implement casts between each other. I only have the DLLs of the two projects and not the code. Can I use extension methods to implement the cast or would I need to do something else?
I'd suggest that you implement your own mappers between the 2 classes or use mapping tools such as AutoMapper or ValueInjecter
You will have to use either extension methods or some other mapping. You could also use http://automapper.codeplex.com/
I don't think there is a way to do it. Anyway, do you really need the code to look like cast? Sometimes when you implement operators or casts for custom types the code may become harder to understand. I would suggest to create separate utility to convert types which would be more obvious for someone who sees the code for the first time.

Categories