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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:21:56+00:00 2026-06-18T14:21:56+00:00

Ok so lets say I have a structure A like that: Struct A{ private

  • 0

Ok so lets say I have a structure A like that:

Struct A{
    private String _SomeText;
    private int _SomeValue;

    public A(String someText, int SomeValue) { /*.. set the initial values..*/ }

    public String SomeText{ get { return _SomeText; } }
    public int SomeValue{ get { return _SomeValue; } }
}

Now what I want to be able to do is to return that Structure A as a result of a method in a Class ABC, like that:

Class ABC{
    public A getStructA(){
        //creation of Struct A
        return a;
    }
}

I don’t want any programmer using my library (which will have Struct A and Class ABC and some more stuff) to ever be able to create an instance of Struct A.
I want the only way for it to be created is as a return from the getStructA() method. Then the values can be accessed through the appropriate getters.

So is there any way to set a restrictions like that? So a Structure can’t be instantiated outside of a certain class? Using C#, .Net4.0.

Thanks for your help.

—EDIT:—-
To add some details on why am I trying to achieve this:

  • My class ABC has some “status” a person can query. This status has 2 string values and then a long list of integers.
  • There never will be a need to create an object/instance of “Status” by the programmer, the status can only be returned by “getStatus()” function of the class.
  • I do not want to split these 3 fields to different methods, as to obtain them I am calling Windows API (p/invoke) which returns similar struct with all 3 fields.
  • If I was indeed going to split it to 3 methods and not use the struct, I would have to either cache results or call the method from Windows API every time one of these 3 methods is called…

So I can either make a public struct and programmers can instantiate it if they want, which will be useless for them as there will be no methods which can accept it as a parameter. Or I can construct the library in such a way that this struct (or change it to a class if it makes things easier) can be obtained only as a return from the method.

  • 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-06-18T14:21:57+00:00Added an answer on June 18, 2026 at 2:21 pm

    If the “restricted” type is a struct, then no, there is no way to do that. The struct must be at least as public as the factory method, and if the struct is public then it can be constructed with its default constructor. However, you can do this:

    public struct A
    {
        private string s;
        private int i;
        internal bool valid;
        internal A(string s, int i)
        {
            this.s = s;
            this.i = i;
            this.valid = true;
        } 
        ...
    

    and now you can have your library code check the “valid” flag. Instances of A can only be made either (1) by a method internal to your library that can call the internal constructor, or (2) by the default constructor. You can tell them apart with the valid flag.

    A number of people have suggested using an interface, but that’s a bit pointless; the whole point of using a struct is to get value type semantics and then you go boxing it into an interface. You might as well make it a class in the first place. If it is going to be a class then it is certainly possible to make a factory method; just make all the ctors of the class internal.

    And of course I hope it goes without saying that none of this gear should be used to implement code that is resistant to attack by a fully-trusted user. Remember, this system is in place to protect good users from bad code, not good code from bad users. There is nothing whatsoever that stops fully trusted user code from calling whatever private methods they want in your library via reflection, or for that matter, altering the bits inside a struct with unsafe code.

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

Sidebar

Related Questions

Let's say I have a first structure like this: typedef struct { int ivalue;
Lets say that I have a database structure with three tables that look like
Lets say I have an array with a structure like this: $arr= Array( array(
Lets say I have a structure struct s { std::deque<Object> q; //won't work with
So, lets say that we have this structure: <div id='allTempLinkHolder'> <div class='tempLink'>Value</div> <div class='tempLink'>Value
Lets say I have the following MySQL structure: CREATE TABLE `domains` ( `id` INT(10)
Lets say have this immutable record type: public class Record { public Record(int x,
Lets say we have the following document structure: class BlogPost { [MongoIdentifier] public Guid
Lets say I have a structure like this: Object A contains an instance of
I am using MySQL ver 5.5.8. Lets say I have the table,entries, structure like

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.