Inline “if”: obtain and test a value in one call
I have the following
IPublishedContentProperty propTitle; // the type is not nullable
// Compiles, 2 GetProperty calls
var title = x.GetProperty("title").HasValue ? x.GetProperty("title").Value : null;
// Does not compile, 1 GetProperty call
title = (propTitle=x.GetProperty("title") && propTitle.HasValue) ?propTitle.Value:null;
Suppose the GetProperty
is a time consuming operation, and I would like to call this method only once.
So, the first line is as it compiles. The second one it does not, but is what I would like to achieve.
Constraints:
- .NET specific version;
- do not use the
if
blocks.
PS. .HasValue
does not mean the type is nullable, is just a type having such a bool property.
c# .net-4.5
|
show 4 more comments
I have the following
IPublishedContentProperty propTitle; // the type is not nullable
// Compiles, 2 GetProperty calls
var title = x.GetProperty("title").HasValue ? x.GetProperty("title").Value : null;
// Does not compile, 1 GetProperty call
title = (propTitle=x.GetProperty("title") && propTitle.HasValue) ?propTitle.Value:null;
Suppose the GetProperty
is a time consuming operation, and I would like to call this method only once.
So, the first line is as it compiles. The second one it does not, but is what I would like to achieve.
Constraints:
- .NET specific version;
- do not use the
if
blocks.
PS. .HasValue
does not mean the type is nullable, is just a type having such a bool property.
c# .net-4.5
2
You do realize that your code is equivalent tovar title = x.GetProperty("title")
?
– Heinzi
Nov 21 '18 at 10:08
You can write extension method that will internally save x.GetProperty into variable and then use condition ?
– dlxeon
Nov 21 '18 at 10:08
no extension methods @Heinzi, is not true, you forget the .Value
– Serge
Nov 21 '18 at 10:09
1
@Serge: Sorry, my fault, it looked likex.GetProperty
returned a nullable value type, but it's apparently something different.
– Heinzi
Nov 21 '18 at 10:10
@Heinzi: good point, is not a nullable type returned.
– Serge
Nov 21 '18 at 10:11
|
show 4 more comments
I have the following
IPublishedContentProperty propTitle; // the type is not nullable
// Compiles, 2 GetProperty calls
var title = x.GetProperty("title").HasValue ? x.GetProperty("title").Value : null;
// Does not compile, 1 GetProperty call
title = (propTitle=x.GetProperty("title") && propTitle.HasValue) ?propTitle.Value:null;
Suppose the GetProperty
is a time consuming operation, and I would like to call this method only once.
So, the first line is as it compiles. The second one it does not, but is what I would like to achieve.
Constraints:
- .NET specific version;
- do not use the
if
blocks.
PS. .HasValue
does not mean the type is nullable, is just a type having such a bool property.
c# .net-4.5
I have the following
IPublishedContentProperty propTitle; // the type is not nullable
// Compiles, 2 GetProperty calls
var title = x.GetProperty("title").HasValue ? x.GetProperty("title").Value : null;
// Does not compile, 1 GetProperty call
title = (propTitle=x.GetProperty("title") && propTitle.HasValue) ?propTitle.Value:null;
Suppose the GetProperty
is a time consuming operation, and I would like to call this method only once.
So, the first line is as it compiles. The second one it does not, but is what I would like to achieve.
Constraints:
- .NET specific version;
- do not use the
if
blocks.
PS. .HasValue
does not mean the type is nullable, is just a type having such a bool property.
c# .net-4.5
c# .net-4.5
edited Nov 21 '18 at 10:28
Serge
asked Nov 21 '18 at 10:05


SergeSerge
3,14733886
3,14733886
2
You do realize that your code is equivalent tovar title = x.GetProperty("title")
?
– Heinzi
Nov 21 '18 at 10:08
You can write extension method that will internally save x.GetProperty into variable and then use condition ?
– dlxeon
Nov 21 '18 at 10:08
no extension methods @Heinzi, is not true, you forget the .Value
– Serge
Nov 21 '18 at 10:09
1
@Serge: Sorry, my fault, it looked likex.GetProperty
returned a nullable value type, but it's apparently something different.
– Heinzi
Nov 21 '18 at 10:10
@Heinzi: good point, is not a nullable type returned.
– Serge
Nov 21 '18 at 10:11
|
show 4 more comments
2
You do realize that your code is equivalent tovar title = x.GetProperty("title")
?
– Heinzi
Nov 21 '18 at 10:08
You can write extension method that will internally save x.GetProperty into variable and then use condition ?
– dlxeon
Nov 21 '18 at 10:08
no extension methods @Heinzi, is not true, you forget the .Value
– Serge
Nov 21 '18 at 10:09
1
@Serge: Sorry, my fault, it looked likex.GetProperty
returned a nullable value type, but it's apparently something different.
– Heinzi
Nov 21 '18 at 10:10
@Heinzi: good point, is not a nullable type returned.
– Serge
Nov 21 '18 at 10:11
2
2
You do realize that your code is equivalent to
var title = x.GetProperty("title")
?– Heinzi
Nov 21 '18 at 10:08
You do realize that your code is equivalent to
var title = x.GetProperty("title")
?– Heinzi
Nov 21 '18 at 10:08
You can write extension method that will internally save x.GetProperty into variable and then use condition ?
– dlxeon
Nov 21 '18 at 10:08
You can write extension method that will internally save x.GetProperty into variable and then use condition ?
– dlxeon
Nov 21 '18 at 10:08
no extension methods @Heinzi, is not true, you forget the .Value
– Serge
Nov 21 '18 at 10:09
no extension methods @Heinzi, is not true, you forget the .Value
– Serge
Nov 21 '18 at 10:09
1
1
@Serge: Sorry, my fault, it looked like
x.GetProperty
returned a nullable value type, but it's apparently something different.– Heinzi
Nov 21 '18 at 10:10
@Serge: Sorry, my fault, it looked like
x.GetProperty
returned a nullable value type, but it's apparently something different.– Heinzi
Nov 21 '18 at 10:10
@Heinzi: good point, is not a nullable type returned.
– Serge
Nov 21 '18 at 10:11
@Heinzi: good point, is not a nullable type returned.
– Serge
Nov 21 '18 at 10:11
|
show 4 more comments
1 Answer
1
active
oldest
votes
The cause for not compiling: &&
is evaluated before the =
. And &&
is obviously not a valid operation on those types.
This can be fixed with a pair of braces. The .HasValue
can then be applied to the result of the assignment (which is the object or value that was assigned).
title = (propTitle = x.GetProperty("title")).HasValue ? propTitle.Value : null;
Edit: you can make this expression shorter and more readable by defining an Extension Method. If you are using the construct in more than one place then it will also reduce redundancy and clutter.
Example:
namespace Your.Project.Helpers
{
public static class PropertyHelper
{
// use actual type (or interface)
public static string GetValueOrDefault(this Property p)
{
return p.HasValue ? p.Value : null;
}
}
}
Usage:
using Your.Project.Helpers;
...
var title = x.GetProperty("title").GetValueOrDefault();
yeah ! great advice, and was so simple!
– Serge
Nov 21 '18 at 10:16
what is a pity, that thepropTitle
should be declared before that code as a separate variable... there is any way to inline declare the variable?
– Serge
Nov 21 '18 at 14:48
See my... extension
– Peter B
Nov 21 '18 at 15:04
good idea, but my case is too specific and not used in other places to really made an extension method. however in general is a good idea. My problem is also I am in the cshtml page that does not need recompilation, but an extension method will need one, so there's dlls to replace... more complex for such a bagatelle )
– Serge
Nov 21 '18 at 15:55
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%2f53409596%2finline-if-obtain-and-test-a-value-in-one-call%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The cause for not compiling: &&
is evaluated before the =
. And &&
is obviously not a valid operation on those types.
This can be fixed with a pair of braces. The .HasValue
can then be applied to the result of the assignment (which is the object or value that was assigned).
title = (propTitle = x.GetProperty("title")).HasValue ? propTitle.Value : null;
Edit: you can make this expression shorter and more readable by defining an Extension Method. If you are using the construct in more than one place then it will also reduce redundancy and clutter.
Example:
namespace Your.Project.Helpers
{
public static class PropertyHelper
{
// use actual type (or interface)
public static string GetValueOrDefault(this Property p)
{
return p.HasValue ? p.Value : null;
}
}
}
Usage:
using Your.Project.Helpers;
...
var title = x.GetProperty("title").GetValueOrDefault();
yeah ! great advice, and was so simple!
– Serge
Nov 21 '18 at 10:16
what is a pity, that thepropTitle
should be declared before that code as a separate variable... there is any way to inline declare the variable?
– Serge
Nov 21 '18 at 14:48
See my... extension
– Peter B
Nov 21 '18 at 15:04
good idea, but my case is too specific and not used in other places to really made an extension method. however in general is a good idea. My problem is also I am in the cshtml page that does not need recompilation, but an extension method will need one, so there's dlls to replace... more complex for such a bagatelle )
– Serge
Nov 21 '18 at 15:55
add a comment |
The cause for not compiling: &&
is evaluated before the =
. And &&
is obviously not a valid operation on those types.
This can be fixed with a pair of braces. The .HasValue
can then be applied to the result of the assignment (which is the object or value that was assigned).
title = (propTitle = x.GetProperty("title")).HasValue ? propTitle.Value : null;
Edit: you can make this expression shorter and more readable by defining an Extension Method. If you are using the construct in more than one place then it will also reduce redundancy and clutter.
Example:
namespace Your.Project.Helpers
{
public static class PropertyHelper
{
// use actual type (or interface)
public static string GetValueOrDefault(this Property p)
{
return p.HasValue ? p.Value : null;
}
}
}
Usage:
using Your.Project.Helpers;
...
var title = x.GetProperty("title").GetValueOrDefault();
yeah ! great advice, and was so simple!
– Serge
Nov 21 '18 at 10:16
what is a pity, that thepropTitle
should be declared before that code as a separate variable... there is any way to inline declare the variable?
– Serge
Nov 21 '18 at 14:48
See my... extension
– Peter B
Nov 21 '18 at 15:04
good idea, but my case is too specific and not used in other places to really made an extension method. however in general is a good idea. My problem is also I am in the cshtml page that does not need recompilation, but an extension method will need one, so there's dlls to replace... more complex for such a bagatelle )
– Serge
Nov 21 '18 at 15:55
add a comment |
The cause for not compiling: &&
is evaluated before the =
. And &&
is obviously not a valid operation on those types.
This can be fixed with a pair of braces. The .HasValue
can then be applied to the result of the assignment (which is the object or value that was assigned).
title = (propTitle = x.GetProperty("title")).HasValue ? propTitle.Value : null;
Edit: you can make this expression shorter and more readable by defining an Extension Method. If you are using the construct in more than one place then it will also reduce redundancy and clutter.
Example:
namespace Your.Project.Helpers
{
public static class PropertyHelper
{
// use actual type (or interface)
public static string GetValueOrDefault(this Property p)
{
return p.HasValue ? p.Value : null;
}
}
}
Usage:
using Your.Project.Helpers;
...
var title = x.GetProperty("title").GetValueOrDefault();
The cause for not compiling: &&
is evaluated before the =
. And &&
is obviously not a valid operation on those types.
This can be fixed with a pair of braces. The .HasValue
can then be applied to the result of the assignment (which is the object or value that was assigned).
title = (propTitle = x.GetProperty("title")).HasValue ? propTitle.Value : null;
Edit: you can make this expression shorter and more readable by defining an Extension Method. If you are using the construct in more than one place then it will also reduce redundancy and clutter.
Example:
namespace Your.Project.Helpers
{
public static class PropertyHelper
{
// use actual type (or interface)
public static string GetValueOrDefault(this Property p)
{
return p.HasValue ? p.Value : null;
}
}
}
Usage:
using Your.Project.Helpers;
...
var title = x.GetProperty("title").GetValueOrDefault();
edited Nov 21 '18 at 15:51


Serge
3,14733886
3,14733886
answered Nov 21 '18 at 10:12


Peter BPeter B
13.1k51943
13.1k51943
yeah ! great advice, and was so simple!
– Serge
Nov 21 '18 at 10:16
what is a pity, that thepropTitle
should be declared before that code as a separate variable... there is any way to inline declare the variable?
– Serge
Nov 21 '18 at 14:48
See my... extension
– Peter B
Nov 21 '18 at 15:04
good idea, but my case is too specific and not used in other places to really made an extension method. however in general is a good idea. My problem is also I am in the cshtml page that does not need recompilation, but an extension method will need one, so there's dlls to replace... more complex for such a bagatelle )
– Serge
Nov 21 '18 at 15:55
add a comment |
yeah ! great advice, and was so simple!
– Serge
Nov 21 '18 at 10:16
what is a pity, that thepropTitle
should be declared before that code as a separate variable... there is any way to inline declare the variable?
– Serge
Nov 21 '18 at 14:48
See my... extension
– Peter B
Nov 21 '18 at 15:04
good idea, but my case is too specific and not used in other places to really made an extension method. however in general is a good idea. My problem is also I am in the cshtml page that does not need recompilation, but an extension method will need one, so there's dlls to replace... more complex for such a bagatelle )
– Serge
Nov 21 '18 at 15:55
yeah ! great advice, and was so simple!
– Serge
Nov 21 '18 at 10:16
yeah ! great advice, and was so simple!
– Serge
Nov 21 '18 at 10:16
what is a pity, that the
propTitle
should be declared before that code as a separate variable... there is any way to inline declare the variable?– Serge
Nov 21 '18 at 14:48
what is a pity, that the
propTitle
should be declared before that code as a separate variable... there is any way to inline declare the variable?– Serge
Nov 21 '18 at 14:48
See my... extension
– Peter B
Nov 21 '18 at 15:04
See my... extension
– Peter B
Nov 21 '18 at 15:04
good idea, but my case is too specific and not used in other places to really made an extension method. however in general is a good idea. My problem is also I am in the cshtml page that does not need recompilation, but an extension method will need one, so there's dlls to replace... more complex for such a bagatelle )
– Serge
Nov 21 '18 at 15:55
good idea, but my case is too specific and not used in other places to really made an extension method. however in general is a good idea. My problem is also I am in the cshtml page that does not need recompilation, but an extension method will need one, so there's dlls to replace... more complex for such a bagatelle )
– Serge
Nov 21 '18 at 15:55
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%2f53409596%2finline-if-obtain-and-test-a-value-in-one-call%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
2
You do realize that your code is equivalent to
var title = x.GetProperty("title")
?– Heinzi
Nov 21 '18 at 10:08
You can write extension method that will internally save x.GetProperty into variable and then use condition ?
– dlxeon
Nov 21 '18 at 10:08
no extension methods @Heinzi, is not true, you forget the .Value
– Serge
Nov 21 '18 at 10:09
1
@Serge: Sorry, my fault, it looked like
x.GetProperty
returned a nullable value type, but it's apparently something different.– Heinzi
Nov 21 '18 at 10:10
@Heinzi: good point, is not a nullable type returned.
– Serge
Nov 21 '18 at 10:11