Byte array Properties C#
I have an object like this:
public class CustomObject{
public byte FieldA {private get; set;}
public IPAddreess FieldB {private get; set;}
}
FieldA is the byte rappresentation of FieldB.
I create this object from two sources of data.
One from a binary file where i need to be fast, then i prefer to set only the FieldA. The other one is in an application where i retrieve the data only in "FieldB format".
I want a function like this:
public IPAddress GetField(){
if (FieldB != null)
return FieldB;
FieldB = new IPAddress(FieldA);
return FieldB;
}
To simplify i used an IPAddress conversion, but usually i have more complex operations to do.
Is this the correct way to do this? Or there is some other method that can simplify this one? I'm using .NET CORE Thank you in advance.
c# properties .net-core byte
add a comment |
I have an object like this:
public class CustomObject{
public byte FieldA {private get; set;}
public IPAddreess FieldB {private get; set;}
}
FieldA is the byte rappresentation of FieldB.
I create this object from two sources of data.
One from a binary file where i need to be fast, then i prefer to set only the FieldA. The other one is in an application where i retrieve the data only in "FieldB format".
I want a function like this:
public IPAddress GetField(){
if (FieldB != null)
return FieldB;
FieldB = new IPAddress(FieldA);
return FieldB;
}
To simplify i used an IPAddress conversion, but usually i have more complex operations to do.
Is this the correct way to do this? Or there is some other method that can simplify this one? I'm using .NET CORE Thank you in advance.
c# properties .net-core byte
Having code in getter / setter methods is generally a bad idea. Not only is code smell, but in the future you will forget there was code in them and it will cause trouble.
– bradbury9
Nov 20 '18 at 8:53
Ok thank you, but if i have a complex operations to do this makes get and set too much large and complex to read, or not?
– Hyruma92
Nov 20 '18 at 8:56
@bradbury9 Why then properties exist at all? You can just use fields instead.
– PetSerAl
Nov 20 '18 at 9:03
@bradbury9 setters do exist to have code in them. They should not have side effects of perform expensive calculations (let alone I/O), but this is fine. See for example Should Properties have side effects.
– CodeCaster
Nov 20 '18 at 9:21
If you need complex operations in your getter/setters you will probably need to rethink your design a bit (events and functions could be needed)
– bradbury9
Nov 20 '18 at 9:32
add a comment |
I have an object like this:
public class CustomObject{
public byte FieldA {private get; set;}
public IPAddreess FieldB {private get; set;}
}
FieldA is the byte rappresentation of FieldB.
I create this object from two sources of data.
One from a binary file where i need to be fast, then i prefer to set only the FieldA. The other one is in an application where i retrieve the data only in "FieldB format".
I want a function like this:
public IPAddress GetField(){
if (FieldB != null)
return FieldB;
FieldB = new IPAddress(FieldA);
return FieldB;
}
To simplify i used an IPAddress conversion, but usually i have more complex operations to do.
Is this the correct way to do this? Or there is some other method that can simplify this one? I'm using .NET CORE Thank you in advance.
c# properties .net-core byte
I have an object like this:
public class CustomObject{
public byte FieldA {private get; set;}
public IPAddreess FieldB {private get; set;}
}
FieldA is the byte rappresentation of FieldB.
I create this object from two sources of data.
One from a binary file where i need to be fast, then i prefer to set only the FieldA. The other one is in an application where i retrieve the data only in "FieldB format".
I want a function like this:
public IPAddress GetField(){
if (FieldB != null)
return FieldB;
FieldB = new IPAddress(FieldA);
return FieldB;
}
To simplify i used an IPAddress conversion, but usually i have more complex operations to do.
Is this the correct way to do this? Or there is some other method that can simplify this one? I'm using .NET CORE Thank you in advance.
c# properties .net-core byte
c# properties .net-core byte
edited Nov 20 '18 at 8:42


John
11.9k32038
11.9k32038
asked Nov 20 '18 at 8:33
Hyruma92Hyruma92
996
996
Having code in getter / setter methods is generally a bad idea. Not only is code smell, but in the future you will forget there was code in them and it will cause trouble.
– bradbury9
Nov 20 '18 at 8:53
Ok thank you, but if i have a complex operations to do this makes get and set too much large and complex to read, or not?
– Hyruma92
Nov 20 '18 at 8:56
@bradbury9 Why then properties exist at all? You can just use fields instead.
– PetSerAl
Nov 20 '18 at 9:03
@bradbury9 setters do exist to have code in them. They should not have side effects of perform expensive calculations (let alone I/O), but this is fine. See for example Should Properties have side effects.
– CodeCaster
Nov 20 '18 at 9:21
If you need complex operations in your getter/setters you will probably need to rethink your design a bit (events and functions could be needed)
– bradbury9
Nov 20 '18 at 9:32
add a comment |
Having code in getter / setter methods is generally a bad idea. Not only is code smell, but in the future you will forget there was code in them and it will cause trouble.
– bradbury9
Nov 20 '18 at 8:53
Ok thank you, but if i have a complex operations to do this makes get and set too much large and complex to read, or not?
– Hyruma92
Nov 20 '18 at 8:56
@bradbury9 Why then properties exist at all? You can just use fields instead.
– PetSerAl
Nov 20 '18 at 9:03
@bradbury9 setters do exist to have code in them. They should not have side effects of perform expensive calculations (let alone I/O), but this is fine. See for example Should Properties have side effects.
– CodeCaster
Nov 20 '18 at 9:21
If you need complex operations in your getter/setters you will probably need to rethink your design a bit (events and functions could be needed)
– bradbury9
Nov 20 '18 at 9:32
Having code in getter / setter methods is generally a bad idea. Not only is code smell, but in the future you will forget there was code in them and it will cause trouble.
– bradbury9
Nov 20 '18 at 8:53
Having code in getter / setter methods is generally a bad idea. Not only is code smell, but in the future you will forget there was code in them and it will cause trouble.
– bradbury9
Nov 20 '18 at 8:53
Ok thank you, but if i have a complex operations to do this makes get and set too much large and complex to read, or not?
– Hyruma92
Nov 20 '18 at 8:56
Ok thank you, but if i have a complex operations to do this makes get and set too much large and complex to read, or not?
– Hyruma92
Nov 20 '18 at 8:56
@bradbury9 Why then properties exist at all? You can just use fields instead.
– PetSerAl
Nov 20 '18 at 9:03
@bradbury9 Why then properties exist at all? You can just use fields instead.
– PetSerAl
Nov 20 '18 at 9:03
@bradbury9 setters do exist to have code in them. They should not have side effects of perform expensive calculations (let alone I/O), but this is fine. See for example Should Properties have side effects.
– CodeCaster
Nov 20 '18 at 9:21
@bradbury9 setters do exist to have code in them. They should not have side effects of perform expensive calculations (let alone I/O), but this is fine. See for example Should Properties have side effects.
– CodeCaster
Nov 20 '18 at 9:21
If you need complex operations in your getter/setters you will probably need to rethink your design a bit (events and functions could be needed)
– bradbury9
Nov 20 '18 at 9:32
If you need complex operations in your getter/setters you will probably need to rethink your design a bit (events and functions could be needed)
– bradbury9
Nov 20 '18 at 9:32
add a comment |
1 Answer
1
active
oldest
votes
You can do that in FieldB
's getter, without explicitly writing a get-method:
private IPAddreess _fieldB;
public IPAddreess FieldB
{
get
{
if (_fieldB == null)
{
_fieldB = new IPAddress(FieldA);
}
return _fieldB;
}
set
{
_fieldB = value;
}
}
This code uses a private backing field _fieldB
for storing the property's value. Upon retrieving the property, it'll either return the value already stored in the field, or assign it based on FieldA
's contents and then return it.
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%2f53388994%2fbyte-array-properties-c-sharp%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
You can do that in FieldB
's getter, without explicitly writing a get-method:
private IPAddreess _fieldB;
public IPAddreess FieldB
{
get
{
if (_fieldB == null)
{
_fieldB = new IPAddress(FieldA);
}
return _fieldB;
}
set
{
_fieldB = value;
}
}
This code uses a private backing field _fieldB
for storing the property's value. Upon retrieving the property, it'll either return the value already stored in the field, or assign it based on FieldA
's contents and then return it.
add a comment |
You can do that in FieldB
's getter, without explicitly writing a get-method:
private IPAddreess _fieldB;
public IPAddreess FieldB
{
get
{
if (_fieldB == null)
{
_fieldB = new IPAddress(FieldA);
}
return _fieldB;
}
set
{
_fieldB = value;
}
}
This code uses a private backing field _fieldB
for storing the property's value. Upon retrieving the property, it'll either return the value already stored in the field, or assign it based on FieldA
's contents and then return it.
add a comment |
You can do that in FieldB
's getter, without explicitly writing a get-method:
private IPAddreess _fieldB;
public IPAddreess FieldB
{
get
{
if (_fieldB == null)
{
_fieldB = new IPAddress(FieldA);
}
return _fieldB;
}
set
{
_fieldB = value;
}
}
This code uses a private backing field _fieldB
for storing the property's value. Upon retrieving the property, it'll either return the value already stored in the field, or assign it based on FieldA
's contents and then return it.
You can do that in FieldB
's getter, without explicitly writing a get-method:
private IPAddreess _fieldB;
public IPAddreess FieldB
{
get
{
if (_fieldB == null)
{
_fieldB = new IPAddress(FieldA);
}
return _fieldB;
}
set
{
_fieldB = value;
}
}
This code uses a private backing field _fieldB
for storing the property's value. Upon retrieving the property, it'll either return the value already stored in the field, or assign it based on FieldA
's contents and then return it.
answered Nov 20 '18 at 8:35
CodeCasterCodeCaster
107k17142193
107k17142193
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%2f53388994%2fbyte-array-properties-c-sharp%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
Having code in getter / setter methods is generally a bad idea. Not only is code smell, but in the future you will forget there was code in them and it will cause trouble.
– bradbury9
Nov 20 '18 at 8:53
Ok thank you, but if i have a complex operations to do this makes get and set too much large and complex to read, or not?
– Hyruma92
Nov 20 '18 at 8:56
@bradbury9 Why then properties exist at all? You can just use fields instead.
– PetSerAl
Nov 20 '18 at 9:03
@bradbury9 setters do exist to have code in them. They should not have side effects of perform expensive calculations (let alone I/O), but this is fine. See for example Should Properties have side effects.
– CodeCaster
Nov 20 '18 at 9:21
If you need complex operations in your getter/setters you will probably need to rethink your design a bit (events and functions could be needed)
– bradbury9
Nov 20 '18 at 9:32