I have an implementation of the Shunting-Yard algorithm that I am trying to integrate clearly into our framework. Currently I have it all packed into a class with a simple public interface.
namespace MathematicalParser {
public class ExpressionParser {
public ExpressionParser(string expression, List<string> variables);
public double GetNumericValue(Dictionary<string,double> variableValues);
}
}
Inside this class there are a lot of helper classes, helper enums, static variables etc to map different names to functions. All these are private so that is no concern to the user of the library.
In an effort to improve maintainability of the code I'm trying to separate code that is logically unrelated into their own classes, these classes do however not have any meaning outside of the ExpressionParser so I'd like to limit their visibility to the namespace MathematicalParser (which only contains the ExpressionParser).
How do you best accomplish this in c#, the internal keyword only works on assemblies and private can't be used in namespaces.
I wouldn't do this (same opinion as Joe), but here's another solution derived from Lopina's answer: nested classes + partial classes.
PublicClass.cs:
namespace MyNamespace
{
public partial class PublicClass
{
public int ReturnSomeStuff()
{
MyHelperClass1 tmp = new MyHelperClass1();
MyHelperClass2 tmp2 = new MyHelperClass2();
return tmp.GetValue1() + tmp2.GetValue2();
}
}
}
PrivateClass1.cs:
namespace MyNamespace
{
public partial class PublicClass
{
private class MyHelperClass1
{
public int GetValue1()
{
return 5;
}
}
}
}
PrivateClass2.cs:
namespace MyNamespace
{
public partial class PublicClass
{
private class MyHelperClass2
{
public int GetValue2()
{
return 10;
}
}
}
}
Program.cs:
public class Program
{
private static void Main(string[] args)
{
PublicClass tmp = new PublicClass();
MyHelperClass2 zz; // Can't access MyHelperClass2 here cause it's private
Console.WriteLine(tmp.ReturnSomeStuff());
Console.ReadLine();
}
}
As you can see, your different helper classes are physically separated in different files (maybe it'll help you maintain your code). And you can't access them directly, they are private to the PublicClass
.