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 ().
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:
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.
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 11 years ago.
Possible Duplicate:
Can attributes be added dynamically in C#?
Is it possible to assign .net Attribute to class/method programmatically?
For example:
Can I decorate my custom .net com classes with Guid/ProgId attributes taken from external file? Something like:
typeof(MyComObject).AssignAttribute(new GuidAttribute("..."));
instead of hardcode like:
[Guid("...")]
class MyComObject
{
}
Thank you in advance!
It depends. ICustomTypeDescriptor allows to almost anything you want to almost every part of a class (which might not even exist for that matter), but this particular interface might not be used by whatever system you're trying to feed your object to. PropertyGrid uses this interface extensively, though.
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.