0

Sorry if the wording of this question is incorrect; I'm coming from a Javascript/Typescript background.

What I'm looking to create is a set of key/value pairs which is immutable, where the IDE/compiler would know which keys are or aren't present in that list. I presume this is possible with a class, and maybe this is really the best option, but I'm interested to understand whether there are alternatives.

For instance, for the config of an application, in Typescript I could have:

const config = { applicationName: 'Some application', port: 8000, }; const testOne = config.applicationName; const testTwo = config.fail; // this would not work 

That is to say, the compiler, or some interpreter which the IDE uses, is able to tell based on the structure that a key/value does not exist for that structure.

It seems to me that a class is excessive for this kind of data; though as I'm relatively new to the language, this may be due to my naivety. I'd like to know if there's a structure within C# that can store keys and values, in an immutable way, and know at compile time whether or not a key/value pair is available in that collection.

12
  • Like in a Dictionary ? Commented Mar 22, 2019 at 12:16
  • 1
    I don't think that the IDE/compiler checks the content of collections. Yes, you are looking to create a class. Commented Mar 22, 2019 at 12:18
  • 1
    Ah I guess I misunderstood your question. You want to know, before compiling if myObj.PropertyThatDoesExist the IDE will show you the error? Commented Mar 22, 2019 at 12:21
  • 1
    I am not sure I understand this question. Did you try using a class? What C# code did you try that didn't give you this functionality? Also, by "would not work", what exactly do you mean? Compiler error? Runtime error? The reason I ask is that out of the box, if you use types with members, this would do exactly what you want. Just create a class with two properties, ApplicationName and Port, then you can't set arbitrary values on it. Commented Mar 22, 2019 at 12:22
  • 1
    The is an XY Problem (Question). That is you're trying to solve a problem using a solution you have a question about instead of actually presenting the real problem. You have all these requirements (like immutable) without describing why they are needed. This is not a good question. Commented Mar 22, 2019 at 12:27

1 Answer 1

3

Use an anonymous type:

var obj = new { applicationName = "", port = 9000 }; var name = obj.applicationName; var something = obj.fail; //doesn't compile 

Although, I really encourage you to create classes and give them a name, as you can expose them from APIs.

Sign up to request clarification or add additional context in comments.

7 Comments

How is this Immutable?
@ErikPhilips you are right, I missed that one. Now I updated the answer to reflect this issue.
This is a useful answer, I'll take a look into anonymous types and structs, and pending any further answers I'll accept this one as correct
@OliverRadini to answer your edited question: no, I don't think that there is such a structure both immutable and anonymous, at least not from what I have seen in my experience with C# until now.
@meJustAndrew Thanks! It seems as though it will be a case of picking from a number of less than ideal alternatives and an anonymous type may be the best option
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.