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 784355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:42:00+00:00 2026-05-14T20:42:00+00:00

public class Foo { public string Name { get; private set;} // <– Because

  • 0
public class Foo
{
    public string Name { get; private set;} // <-- Because set is private,
}

void Main()
{
    var bar = new Foo {Name = "baz"}; // <-- This doesn't compile
    /*The property or indexer 'UserQuery.Foo.Name' cannot be used 
      in this context because the set accessor is inaccessible*/

    using (DataContext dc = new DataContext(Connection))
    {
        // yet the following line works.  **How**?
        IEnumerable<Foo> qux = dc.ExecuteQuery<Foo>(
           "SELECT Name FROM Customer");
    }
    foreach (q in qux) Console.WriteLine(q);
}

I have just been using the private modifier because it works and kept me from being stupid with my code, but now that I need to create a new Foo, I’ve just removed the private modifier from my property. I’m just really curious, why does the ExecuteQuery into an IEnumerable of Foo’s work?

EDIT Ok, so the private modifier doesn’t keep reflection from seeing the setter, and from the answers, it appears that ExecuteQuery (or is it the data context?) uses reflection to get property names and ignores the modifiers. Is there a way to verify that? How could I have figured that out on my own? (adding reflection to the tag list)

  • 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-14T20:42:01+00:00Added an answer on May 14, 2026 at 8:42 pm

    Create a constructor on Foo that accepts a value for “Name”:

    public class Foo
    {
        public Foo(string name)
        {
            Name = name;
        }
    
        public string Name { get; private set; }
    }
    

    Now construct your Foo like this:

    var bar = new Foo("baz");
    

    Edit (read the rest of your question)

    My guess is that ExecuteQuery uses reflection to inspect the class and find its properties. It probably doesn’t care that the setter on Name is private – only that Name has a setter at all.

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

Sidebar

Ask A Question

Stats

  • Questions 506k
  • Answers 506k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer array = [] is a shortcut to define an array… May 16, 2026 at 3:52 pm
  • Editorial Team
    Editorial Team added an answer Founded how. In CPTBarPlotDataSource protocol there is a -(CPTFill *)barFillForBarPlot:(CPTBarPlot… May 16, 2026 at 3:52 pm
  • Editorial Team
    Editorial Team added an answer DataSet.GetXml() method gives xml string - you could have use… May 16, 2026 at 3:52 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a class as follows Public Class Foo Private _Name As String <ShowInDisplay()>
I imagine to use XML serialization like this: class Foo { public Foo (string
If you have an object like this: public class Foo { private int _id;
Consider the following: var o = new { Foo = foo, Bar = bar
I have an abstract generic class public abstract class Foo<TType> with an abstract method
I have a class Foo with a field UpdateMe of type Confirmation as described
So obviously I am doing something wrong, but I just cannot seem to get
I'm probably doing something silly, but here it goes. I'm trying to get the
I am currently struggling to get the following code to compile. First the header
#include <iostream> #include <set> #include <algorithm> #include <boost/lambda/lambda.hpp> #include <boost/bind.hpp> using namespace std; using

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.