This question already has answers here:
Is there a generic constructor with parameter constraint in C#?
(9 answers)
Closed 8 years ago.
I wonder, what rationale is behind lack of generic class type constraints for typed constructors? eg.
public class MyClass<T>
where T : new(int)
{
public T Create(int i)
{
return new T(i);
}
}
Despite fact, that this may be quite easily (though IMO ugly) bypassed (by lambda-ctor), I can imagine no situation, when this constraint might cause any actual trouble or ambiguities.
Notice, that this is a language-structure question, not about a specific problem.
I searched a little bit and found an answer. But since it is here on SO and I don't want to copy it, I will just post a link. It is an answer from Eric Lippert. I hope his answers means something to you.
https://stackoverflow.com/a/9741812/809009
It is somewhat long question there, but you can skip it and read only linked answer.
Related
This question already has answers here:
Why string is sealed
(3 answers)
Closed 2 years ago.
i read some articles but most of them are abstracted and i can't get answering of my questions.
i know the difference between Abstract and Sealed Classes.but when my instructor explained the difference he said in c# when you want to make some developers to follow your design you need to use Abstract class and this give some safety of your code and gave examples and i understand what he means but when he explained sealed class he said we can't inherit from string class because it's sealed class. so that's using of sealed classes and didn't give the reason.
A sufficient reason why String is sealed is that String is a performance-critical class because most programs use it heavily. And so it's heavily optimized. Virtual method calls have some additional overhead, as it must be determined at runtime which method to actually call, the base-type method, or some override.
This question already has answers here:
C# Empty Statement
(13 answers)
Closed 5 years ago.
I've come across this example of an empty statement in a C# textbook.
Code:
public void empty()
{
;
}
Some quick googling found that it's a redundant feature and I can't see the use of this as it seems pointless?
I was curious to know when this would've been useful and if it's still used to date even though it's obsolete?
In the given example it is pointless and/or cosmetic.
The empty statement is "useful" in places where a statement is required but you have nothing to do, like
while (condition_with_side_effects) ;
Because of the side effects required, this will not match with most coding guidelines or best practices.
Consider it a leftover from C.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why it is not posible to define generic indexers in .NET?
how to write a function to take any object with an index operator
I've never seen any usage like that. But I just wonder if it is possible to make an implementation like bleow. I know that it's not working. But I mean a similar usage if exist.
public T this<T>[T param]
{
get
{
....
}
}
No, generic properties, and indexers (a property), aren't possible.
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."
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
is there a difference between [Serializable] and [Serializable()] in c#?
I always use [Serializable()] in my WebServices, before a class declaration, to serialize it. Today in a tutorial I see [Serializable]...
What are the differences?
Nothing.
If you had needed to pass parameters, you would have need the first syntax, though.
There is any functional difference actually.
The allowed 2 different types cause several arguments can have a parameters, so its tru also in this case:
can have a look on this answer: is there a difference between [Serializable] and [Serializable()] in c#?
There is no difference between them since the constructor of SerializableAttribute accepts no parameters.
On the otherhand, if any attribute constructor accepts parameters, they should be written inside ().