Static string interpolation
public static class SomeRegexConsts
{
public static readonly string FullName = $"{Name} {Surname}";
private static readonly string Name = "[A-Z][a-z]+";
private static readonly string Surname = "[A-Z][a-z]+";
}
In above example FullName will be equal to " " at runtime.
It's related to the way static fields are initialized (from top to bottom).
In general I don't see any other solution than:
public static string FullName => $"{Name} {Surname}";
Any ideas how to improve this code as I don't like this lambda, and moving FullName below Name and Surname is not an option as StyleCop won't let me do this.
c# string static interpolation
|
show 3 more comments
public static class SomeRegexConsts
{
public static readonly string FullName = $"{Name} {Surname}";
private static readonly string Name = "[A-Z][a-z]+";
private static readonly string Surname = "[A-Z][a-z]+";
}
In above example FullName will be equal to " " at runtime.
It's related to the way static fields are initialized (from top to bottom).
In general I don't see any other solution than:
public static string FullName => $"{Name} {Surname}";
Any ideas how to improve this code as I don't like this lambda, and moving FullName below Name and Surname is not an option as StyleCop won't let me do this.
c# string static interpolation
1
I hope this is just a simplified example, because those regex patterns won't match names in the real world.
– Ben Voigt
Jan 2 at 14:53
Oh, yes, it is simplified. Actual regex is way longer but it is not a topic of this question
– Random Guy
Jan 2 at 14:55
You do realize that there are names that have letters outside the range ofA-Z?
– Rand Random
Jan 2 at 15:03
Yes I do, but probably you do not realize that I've already answered this question :)
– Random Guy
Jan 2 at 15:06
Who reads comments? Sorry, about asking the same question again. :)
– Rand Random
Jan 2 at 15:07
|
show 3 more comments
public static class SomeRegexConsts
{
public static readonly string FullName = $"{Name} {Surname}";
private static readonly string Name = "[A-Z][a-z]+";
private static readonly string Surname = "[A-Z][a-z]+";
}
In above example FullName will be equal to " " at runtime.
It's related to the way static fields are initialized (from top to bottom).
In general I don't see any other solution than:
public static string FullName => $"{Name} {Surname}";
Any ideas how to improve this code as I don't like this lambda, and moving FullName below Name and Surname is not an option as StyleCop won't let me do this.
c# string static interpolation
public static class SomeRegexConsts
{
public static readonly string FullName = $"{Name} {Surname}";
private static readonly string Name = "[A-Z][a-z]+";
private static readonly string Surname = "[A-Z][a-z]+";
}
In above example FullName will be equal to " " at runtime.
It's related to the way static fields are initialized (from top to bottom).
In general I don't see any other solution than:
public static string FullName => $"{Name} {Surname}";
Any ideas how to improve this code as I don't like this lambda, and moving FullName below Name and Surname is not an option as StyleCop won't let me do this.
c# string static interpolation
c# string static interpolation
edited Jan 2 at 15:10
Rand Random
3,09873164
3,09873164
asked Jan 2 at 14:36
Random GuyRandom Guy
85
85
1
I hope this is just a simplified example, because those regex patterns won't match names in the real world.
– Ben Voigt
Jan 2 at 14:53
Oh, yes, it is simplified. Actual regex is way longer but it is not a topic of this question
– Random Guy
Jan 2 at 14:55
You do realize that there are names that have letters outside the range ofA-Z?
– Rand Random
Jan 2 at 15:03
Yes I do, but probably you do not realize that I've already answered this question :)
– Random Guy
Jan 2 at 15:06
Who reads comments? Sorry, about asking the same question again. :)
– Rand Random
Jan 2 at 15:07
|
show 3 more comments
1
I hope this is just a simplified example, because those regex patterns won't match names in the real world.
– Ben Voigt
Jan 2 at 14:53
Oh, yes, it is simplified. Actual regex is way longer but it is not a topic of this question
– Random Guy
Jan 2 at 14:55
You do realize that there are names that have letters outside the range ofA-Z?
– Rand Random
Jan 2 at 15:03
Yes I do, but probably you do not realize that I've already answered this question :)
– Random Guy
Jan 2 at 15:06
Who reads comments? Sorry, about asking the same question again. :)
– Rand Random
Jan 2 at 15:07
1
1
I hope this is just a simplified example, because those regex patterns won't match names in the real world.
– Ben Voigt
Jan 2 at 14:53
I hope this is just a simplified example, because those regex patterns won't match names in the real world.
– Ben Voigt
Jan 2 at 14:53
Oh, yes, it is simplified. Actual regex is way longer but it is not a topic of this question
– Random Guy
Jan 2 at 14:55
Oh, yes, it is simplified. Actual regex is way longer but it is not a topic of this question
– Random Guy
Jan 2 at 14:55
You do realize that there are names that have letters outside the range of
A-Z?– Rand Random
Jan 2 at 15:03
You do realize that there are names that have letters outside the range of
A-Z?– Rand Random
Jan 2 at 15:03
Yes I do, but probably you do not realize that I've already answered this question :)
– Random Guy
Jan 2 at 15:06
Yes I do, but probably you do not realize that I've already answered this question :)
– Random Guy
Jan 2 at 15:06
Who reads comments? Sorry, about asking the same question again. :)
– Rand Random
Jan 2 at 15:07
Who reads comments? Sorry, about asking the same question again. :)
– Rand Random
Jan 2 at 15:07
|
show 3 more comments
3 Answers
3
active
oldest
votes
If you don't want to move FullName below others, I think this's only solution for you.
public static class SomeRegexConsts
{
static SomeRegexConsts(){
FullName = $"{Name} {Surname}";
}
public static readonly string FullName;
private static readonly string Name = "[A-Z][a-z]+";
private static readonly string Surname = "[A-Z][a-z]+";
1
Looks good to me. I've totally forgot about static ctors :)
– Random Guy
Jan 2 at 14:47
add a comment |
What about creating a public method that returns the FullName string ?
public string getFullname()
{
return $"{Name} {Surname}";
}
This way public static readonly string FullName = $"{Name} {Surname}";, if it's ok to get rid of it, won't be needed anymore.
yes, but it adds another method invocation for something that won't change
– Random Guy
Jan 2 at 14:50
add a comment |
I believe this would work with StyleCop since it requires const before fields of any type:
public static class SomeRegexConsts
{
private const string Name = "[A-Z][a-z]+";
private const string Surname = "[A-Z][a-z]+";
public static readonly string FullName = $"{Name} {Surname}";
}
unless you're using reflection within the class that requires a non-const field, the compiled code should be unaffected.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54008215%2fstatic-string-interpolation%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you don't want to move FullName below others, I think this's only solution for you.
public static class SomeRegexConsts
{
static SomeRegexConsts(){
FullName = $"{Name} {Surname}";
}
public static readonly string FullName;
private static readonly string Name = "[A-Z][a-z]+";
private static readonly string Surname = "[A-Z][a-z]+";
1
Looks good to me. I've totally forgot about static ctors :)
– Random Guy
Jan 2 at 14:47
add a comment |
If you don't want to move FullName below others, I think this's only solution for you.
public static class SomeRegexConsts
{
static SomeRegexConsts(){
FullName = $"{Name} {Surname}";
}
public static readonly string FullName;
private static readonly string Name = "[A-Z][a-z]+";
private static readonly string Surname = "[A-Z][a-z]+";
1
Looks good to me. I've totally forgot about static ctors :)
– Random Guy
Jan 2 at 14:47
add a comment |
If you don't want to move FullName below others, I think this's only solution for you.
public static class SomeRegexConsts
{
static SomeRegexConsts(){
FullName = $"{Name} {Surname}";
}
public static readonly string FullName;
private static readonly string Name = "[A-Z][a-z]+";
private static readonly string Surname = "[A-Z][a-z]+";
If you don't want to move FullName below others, I think this's only solution for you.
public static class SomeRegexConsts
{
static SomeRegexConsts(){
FullName = $"{Name} {Surname}";
}
public static readonly string FullName;
private static readonly string Name = "[A-Z][a-z]+";
private static readonly string Surname = "[A-Z][a-z]+";
answered Jan 2 at 14:42
Antoine VAntoine V
5,1142425
5,1142425
1
Looks good to me. I've totally forgot about static ctors :)
– Random Guy
Jan 2 at 14:47
add a comment |
1
Looks good to me. I've totally forgot about static ctors :)
– Random Guy
Jan 2 at 14:47
1
1
Looks good to me. I've totally forgot about static ctors :)
– Random Guy
Jan 2 at 14:47
Looks good to me. I've totally forgot about static ctors :)
– Random Guy
Jan 2 at 14:47
add a comment |
What about creating a public method that returns the FullName string ?
public string getFullname()
{
return $"{Name} {Surname}";
}
This way public static readonly string FullName = $"{Name} {Surname}";, if it's ok to get rid of it, won't be needed anymore.
yes, but it adds another method invocation for something that won't change
– Random Guy
Jan 2 at 14:50
add a comment |
What about creating a public method that returns the FullName string ?
public string getFullname()
{
return $"{Name} {Surname}";
}
This way public static readonly string FullName = $"{Name} {Surname}";, if it's ok to get rid of it, won't be needed anymore.
yes, but it adds another method invocation for something that won't change
– Random Guy
Jan 2 at 14:50
add a comment |
What about creating a public method that returns the FullName string ?
public string getFullname()
{
return $"{Name} {Surname}";
}
This way public static readonly string FullName = $"{Name} {Surname}";, if it's ok to get rid of it, won't be needed anymore.
What about creating a public method that returns the FullName string ?
public string getFullname()
{
return $"{Name} {Surname}";
}
This way public static readonly string FullName = $"{Name} {Surname}";, if it's ok to get rid of it, won't be needed anymore.
answered Jan 2 at 14:43
Noob devNoob dev
869
869
yes, but it adds another method invocation for something that won't change
– Random Guy
Jan 2 at 14:50
add a comment |
yes, but it adds another method invocation for something that won't change
– Random Guy
Jan 2 at 14:50
yes, but it adds another method invocation for something that won't change
– Random Guy
Jan 2 at 14:50
yes, but it adds another method invocation for something that won't change
– Random Guy
Jan 2 at 14:50
add a comment |
I believe this would work with StyleCop since it requires const before fields of any type:
public static class SomeRegexConsts
{
private const string Name = "[A-Z][a-z]+";
private const string Surname = "[A-Z][a-z]+";
public static readonly string FullName = $"{Name} {Surname}";
}
unless you're using reflection within the class that requires a non-const field, the compiled code should be unaffected.
add a comment |
I believe this would work with StyleCop since it requires const before fields of any type:
public static class SomeRegexConsts
{
private const string Name = "[A-Z][a-z]+";
private const string Surname = "[A-Z][a-z]+";
public static readonly string FullName = $"{Name} {Surname}";
}
unless you're using reflection within the class that requires a non-const field, the compiled code should be unaffected.
add a comment |
I believe this would work with StyleCop since it requires const before fields of any type:
public static class SomeRegexConsts
{
private const string Name = "[A-Z][a-z]+";
private const string Surname = "[A-Z][a-z]+";
public static readonly string FullName = $"{Name} {Surname}";
}
unless you're using reflection within the class that requires a non-const field, the compiled code should be unaffected.
I believe this would work with StyleCop since it requires const before fields of any type:
public static class SomeRegexConsts
{
private const string Name = "[A-Z][a-z]+";
private const string Surname = "[A-Z][a-z]+";
public static readonly string FullName = $"{Name} {Surname}";
}
unless you're using reflection within the class that requires a non-const field, the compiled code should be unaffected.
answered Jan 2 at 15:16
D StanleyD Stanley
124k9116180
124k9116180
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54008215%2fstatic-string-interpolation%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
I hope this is just a simplified example, because those regex patterns won't match names in the real world.
– Ben Voigt
Jan 2 at 14:53
Oh, yes, it is simplified. Actual regex is way longer but it is not a topic of this question
– Random Guy
Jan 2 at 14:55
You do realize that there are names that have letters outside the range of
A-Z?– Rand Random
Jan 2 at 15:03
Yes I do, but probably you do not realize that I've already answered this question :)
– Random Guy
Jan 2 at 15:06
Who reads comments? Sorry, about asking the same question again. :)
– Rand Random
Jan 2 at 15:07