Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6358507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:23:20+00:00 2026-05-24T23:23:20+00:00

So I’ve been studying the use of various Serializers in the .NET Framework and

  • 0

So I’ve been studying the use of various Serializers in the .NET Framework and while trying to experiment on preventing certain objects in a class from being serialized I was thrusted back to some very basic programming questions that I “thought” I knew. Given this example:

public class Example
{
   public string examName;
   [XmlIgnore]
   public int exampleNumber;

   public Example()
   { }
   [XmlIgnore]
   public int ExampleNumberTwo { get; set; }
}

I can create an instance of this class and using the XMLSerializer can output the content of this class in XML format. The [XmlIgnore] attribute actually does what I’d expected; it prevents the serialization of the referenced items.

So venturing further I replaced the [XmlIgnore] declaration for “exampleNumber” with [NonSerializable] expecting the similar results but the output did not change. After searching through resources, it was stated that the [NonSerializable] attribute should only be used on fields and [XmlIgnore] attributes should be used on properties.

Yet another post stated that the [NonSerializable] attribute has no effect when using the XMLSerializer but will produce the expected results when using the SOAP or BinaryFormatter. So I’m lost on the concept at this point.

But this brought me to the basic question, what defines a field vs. a property? I know its a basic question and I’ve even viewed other discussions here but the degree of clarity I am looking for still wasn’t really clear.

I can use the [XmlIgnore] attribute on the property (ExampleNumberTwo) or the variable (exampleNumber) so the statement that it can ONLY be used on Properties doesn’t seem correct.

But then again, I have always referred to the objects in my example such as (examName) and (exampleNumber) as being member variables. So what exactly is the signature of a “Field”

Can anyone shed some light on this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T23:23:21+00:00Added an answer on May 24, 2026 at 11:23 pm

    The MSDN documentation supports the idea that [NonSerialized] only gives the expected results with the binary and SOAP serializers:

    When using the BinaryFormatter or SoapFormatter classes to serialize
    an object, use the NonSerializedAttribute attribute to prevent a field
    from being serialized.
    For example, you can use this attribute to
    prevent the serialization of sensitive data.

    The target objects for the NonSerializedAttribute attribute are public
    and private fields of a serializable class. By default, classes are
    not serializable unless they are marked with SerializableAttribute.
    During the serialization process all the public and private fields of
    a class are serialized by default. Fields marked with
    NonSerializedAttribute are excluded during serialization. If you are
    using the XmlSerializer class to serialize an object, use the
    XmlIgnoreAttribute class to get the same functionality.
    Alternatively,
    implement the ISerializable interface to explicitly control the
    serialization process. Note that classes that implement ISerializable
    must still be marked with SerializableAttribute.

    In terms of “field” vs. “property”, fields are straight data variables contained by a class. Properties are actually specially named methods on the class (get_PropName() and set_PropName()). In your code, the compiler allows you to use properties the same way you would use a field, and then inserts the appropriate get/set call for you.

    Oftentimes, properties will be simple wrappers around a field:

    private int myField;
    public int MyProperty
    {
        get { return myField; }
        set { myField = value; }
    }
    

    But they don’t have to be:

    public int TodaysDate
    {
        get { return DateTime.Today; }
    }
    

    In general, you want all your fields to be private, since they’re supposed to be implementation details. Any simple data that you’d like to expose should be done via a property, since you can easily surround the data access with (changeable) logic.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I am doing a simple coin flipping experiment for class that involves flipping a
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.