Byte array Properties C#











up vote
0
down vote

favorite
1












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.










share|improve this question
























  • 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 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 at 8:56










  • @bradbury9 Why then properties exist at all? You can just use fields instead.
    – PetSerAl
    Nov 20 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 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 at 9:32















up vote
0
down vote

favorite
1












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.










share|improve this question
























  • 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 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 at 8:56










  • @bradbury9 Why then properties exist at all? You can just use fields instead.
    – PetSerAl
    Nov 20 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 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 at 9:32













up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 8:42









John

10.8k31736




10.8k31736










asked Nov 20 at 8:33









Hyruma92

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 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 at 8:56










  • @bradbury9 Why then properties exist at all? You can just use fields instead.
    – PetSerAl
    Nov 20 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 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 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 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 at 8:56










  • @bradbury9 Why then properties exist at all? You can just use fields instead.
    – PetSerAl
    Nov 20 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 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 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 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 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 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 at 8:56












@bradbury9 Why then properties exist at all? You can just use fields instead.
– PetSerAl
Nov 20 at 9:03




@bradbury9 Why then properties exist at all? You can just use fields instead.
– PetSerAl
Nov 20 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 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 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 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 at 9:32












1 Answer
1






active

oldest

votes

















up vote
2
down vote













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.






share|improve this answer





















    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',
    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
    });


    }
    });














    draft saved

    draft discarded


















    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








    up vote
    2
    down vote













    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.






    share|improve this answer

























      up vote
      2
      down vote













      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.






      share|improve this answer























        up vote
        2
        down vote










        up vote
        2
        down vote









        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 at 8:35









        CodeCaster

        106k17139190




        106k17139190






























            draft saved

            draft discarded




















































            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'