This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How do you give a C# Auto-Property a default value?
Hi all:
If I want to add default value, says 60, to following property for a class, what is the most convenient way in C#3.0
public int someSetting { get; set; }
You need to initialize it in all constructors.
Put it in the parameterless constructor, that's what I'd do.
Edit: Yes, I guess what I mean by the above is any constructor where the parameter in question is not passed in.
I would think the best way is to set the default in the constructor of the class
Related
This question already has answers here:
C# nameof generic type without specifying type
(3 answers)
Closed 10 days ago.
I have a class:
public class StateMachine<TTag> where TTag : Enum { ... }
Which logs using Log that accepts as first argument string tag. I use nameof with StateMachine<TTag> as an argument:
_logger.Log(nameof(StateMachine<TTag>), ...)
In my app I have instances of StateMachine<TTag>, like StateMachine<AppStateTag>, etc.
And I want my StateMachine to log like this:
StateMachine<AppStateTag> ...
But instead I get:
StateMachine ...
Do I need to get weird like this?
${nameof(StateMachine)}<{nameof(TTag)}>
Is there any simple way of doing this?
So, ok, using answers from here, I've managed to frankenstein this:
$"{nameof(StateMachine<TTag>)}<{typeof(TTag).Name}>"
If anyone can suggest a better way of doing this, it would be very much appreciated.
This question already has answers here:
Can attributes be added dynamically in C#?
(10 answers)
Closed last year.
I'm trying to make a generic method and I want to pass an attribute when call it. Is it possibile or it's possible only on declaration?
Code:
public Window()
{
//Is it possible do this?:
[CustomAttribute(typeof(Something))]
MyGenericMethod();
}
private void MyGenericMethod()
{
//do stuff...
//retrieve type from CustomAttribute
}
public class CustomAttribute : Attribute
{
public CustomAttribute(Type type)
{
}
}
The short answer is NO. An attribute is used to associate metadata, or add declarative information. So you can't call an attribute when you are invoking your method. You can read more about them here: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/attributes/
I consider that you will have to review your solution for your problem and find a different approach.
I don't think it's possible but you can use generic methods.
https://learn.microsoft.com/it-it/dotnet/csharp/programming-guide/generics/generic-methods
This question already has answers here:
What is the difference between getter-only auto properties and expression body properties?
(2 answers)
Closed 7 years ago.
I have a class that uses some services. I need each service to be instantiated on creation of the class. In C# 6 I can see 2 ways of doing this but I'm not sure which would be correct...
protected static SomeServiceType Service => new SomeServiceType();
alternatively I could use an autoproperty initialiser...
protected static SomeServiceType Service { get;} = new SomeServiceType();
What are the advantages/drawbacks with each approach?
Many thanks
I believe
the former ("Expression-bodied members") calls new SomeServiceType() every time the property is read
the latter ("Auto-property initializers") calls it once on instantiation, and returns the created instance every time the property is read.
It sounds like you want the latter.
This question already has answers here:
Add new property to string class C# [duplicate]
(3 answers)
Closed 7 years ago.
I don't like the way string behaves and there are a few other things I would like to change.
It appears string cannot be extended because it is a sealed class.
Is there another way? I could copy the source code and make my own class but then it wouldn't be compatible with string, or could I make it compatible?
You could use extension methods to extend String. The link below explains extension methods and has an example of how to add a WordCount() function to String.
https://msdn.microsoft.com/en-us/library/bb383977.aspx
This question already has answers here:
What is the difference between a field and a property?
(33 answers)
Closed 8 years ago.
Suppose I have a property in a class as in the following:
class testclass
{
public string Name {get; set;}
public void dosomething(){//...}
}
There is no functional difference between this format and the following:
class testclass
{
public string name;
public void dosomething(){//...}
}
Both name fields can be set to anything including an empty string and both can retrieve just without any restrictions. So what is the use of the property semantics detailed above where there is no validation or other process in the get and set methods? One use I see is that you can remove either the get or set method to make it write only or read only, respectively. I don't know what other use this would serve.
The main reason is so that you can change the implementation later without breaking client code. You might not do any validation or raise an event now but what if you decide to in the future? Also, properties can be bound while fields can't.