-1

I have a class definition in which a readonly member is defined.

private readonly Dictionary<string, string> map = new Dictionary<string, string>();

Now in order to test my design, I want to access this member outside its class definition. I was thinking of providing a get method but unable to write an error free syntax.

Is it possible to assign value to a member(using new) and still able to define its get method?

PS: I am new to C# language.

EDIT: I have not written the code, its just a statement I have copied from an already written module. I have made some design changes in the module and want to test it with minimal changes possible in the code, so for that I was looking to get the readonly access of this member outside the class.

Ravi Gupta
  • 6,258
  • 17
  • 56
  • 79
  • 3
    It is normally a bad idea to expose internal reference types directly - consider returning a copy of the dictionary from either a property or method to ensure it will not be modified by its users. – Oded Dec 12 '12 at 14:02
  • 3
    Are you trying to provide read only access? Are you aware that a readonly dictionary is still mutable? – Mark Byers Dec 12 '12 at 14:02
  • 3
    what exactly do you want to test? the `readonly` keyword means, that the variable is only assignable in the constructor. not that you can't add or delete values/keys. – esskar Dec 12 '12 at 14:04
  • @MarkByers: Yes, I want to provide readonly access, but don't know anything about mutable stuff as I am new to C#. – Ravi Gupta Dec 12 '12 at 14:08
  • @RaviGupta: It is important to understand the internals first. http://en.wikipedia.org/wiki/Immutable_object –  Dec 12 '12 at 14:10

5 Answers5

2

You can define a read-only property for permitting public access to your field:

private readonly Dictionary<string, string> map = 
    new Dictionary<string, string>();

public Dictionary<string, string> Map
{
    get { return map; }
}

Note that this will only prevent external classes from changing the instance reference assigned to map, not from changing the content of the dictionary itself.

Douglas
  • 53,759
  • 13
  • 140
  • 188
2

One could argue that you shouldn't write tests for private members of a class. Tests should only use the public interface and don't rely on the internals of the class, since you should be able to refactor internals of the class without breaking any tests.

If you add a public getter 'only for testing' there's no guarantee that someone will start using somewhere in the project.

If you really want to expose the dictionary and use .NET 4.5, use ReadOnlyDictionary class to make sure that the caller won't change anything.

public IDictionary<string, string> Map
{
    get { return new ReadOnlyDictionary<string, string>(map); }
}
Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126
0

Just create a simple getter

public  Dictionary<string, string> Mapping
{
   get { return map; }
}
Erix
  • 7,059
  • 2
  • 35
  • 61
0

You mean something like this?

readonly Dictionary<string, string> _map = new Dictionary<string, string>();

public Dictionary<string, string> Map
{
    get { return _map; }
}

Why are you making this variable readonly?


If you're trying to give access to the values contained in the dictionary you could create a method that exposes the dictionary without allowing it to be modified:

public string GetMapValue(string key)
{
    return _map[key];
}
hunter
  • 62,308
  • 19
  • 113
  • 113
  • Only problem with this is that the internal dictionary is now externally modifiable. – Oded Dec 12 '12 at 14:01
  • @Oded: I don't think readonly has the same effect on a Dictionary in .NET. It's still mutable. http://stackoverflow.com/questions/678379/is-there-a-read-only-generic-dictionary-available-in-net –  Dec 12 '12 at 14:04
  • @0A0D - True, but returning a copy will still keep the internal dictionary safe. Your answer suffers from the same issue (seeing as it is identical...) – Oded Dec 12 '12 at 14:05
  • @Oded: That's why you use a ReadOnlyDictionary :) http://msdn.microsoft.com/en-us/library/gg712875.aspx –  Dec 12 '12 at 14:06
  • @0A0D - To expand. The _reference_ is immutable, not the dictionary. This is true for all reference types - their members tend to also be mutable (depending on how they were written). – Oded Dec 12 '12 at 14:06
  • @Oded: Oh, of course. I wanted to make sure it was clarified for future readers. Though C# does not use pointers directly, you almost need to understand how pointers work to understand reference types. –  Dec 12 '12 at 14:08
0

You could write a public property function to return the private readonly map, e.g:

public Dictionary<string, string> Map { { get { return map; } } }

However, the Dictionary is still mutable. If you want a read-only Dictionary, see this SO question.

Community
  • 1
  • 1