Shaun Luttin
Shaun Luttin

Reputation: 141552

What namespaces are predefined in the C# language?

I'm trying to figure out which namespaces (not assemblies) are predefined in C#.

Start Edit

Define the term predefined.

The term predefined (or pre-defined) is used over 100 times in the C# language specification. Unfortunately, the specification never gives a formal definition of the term. To me, something is predefined if the language specification defines (i.e. specifies) it and guarantees that it will be present.

End Edit

For instance, I've created the following program.

class Program
{
    static void Main(string[] args)
    {
        var strings = new System.Collections.Generic.List<string> { 
            "Foo",
            "Bar"
        };
        strings.ForEach((s) =>
        {
            System.Console.WriteLine(s);
        });
        System.Console.ReadKey();
    }
}

This is its .csproj file.

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <OutputPath>bin\</OutputPath>
  </PropertyGroup>
  <ItemGroup>
    <Compile Include="Program.cs" />
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

Visual Studio is not involved at all. The above project has only two files. It builds with msbuild on the command line and runs as a console app.

Even though I am not referencing any assemblies, the System.Console and System.Collections.List<T> classes are available. So, I assume that the System and System.Collections namespaces are predefined in the C# language.

I've been reading the C# language specification to determine which namespaces are predefined. It does list some predefined types, conversions, and operators. It doesn't list all the predefined namespaces though.

What namespaces are predefined in the C# language?

Edit

Xanatos provided code that I placed into a fiddle. It demonstrates that the mscorlib contains 56 namespaces.

Upvotes: 1

Views: 1914

Answers (2)

xanatos
xanatos

Reputation: 111870

You are mixing namespaces and assemblies. Assemblies are containers of compiled code. .NET has many assemblies built by Microsoft (mscorlib, System, System.Core, System.Data ...) that are part of the Common Language Runtime.

Each assembly has some classes/structs inside. Normally these classes are grouped in namespaces. It isn't strictly necessary, but Microsoft, being good, did it. Now, these namespaces have often the same name of assemblies (System, System.Data) (but note that there is no namespace System.Core or mscorlib). There is no strict relationship between the naming of assemblies and the namespaces defined inside.

A namespace can be defined in multiple assemblies (System for example is present in both mscorlib, System, System.Core ...). An assembly can have classes with multiple namespaces (mscorlib has classes of System, System.Collections, System.IO, ...)

When you compile a C# program, the mscorlib assembly is automatically referenced as an assembly (and I don't think it is possible to compile without referencing it). There is no default namespace in C# (you have to use the using to use them, or use the full name of the classes, like System.Console)

I suggest you install ILSpy and look at the various assemblies to comprehend how they are organized.

On MSDN, if you look at any one class/struct of .NET (for example System.Int32) you'll see:

Namespace: System

Assembly: mscorlib (in mscorlib.dll)

(I'll say that the ordering is conceptually wrong, because the Assembly should be first, but it isn't really important)

If you really want a list of namespaces "exported" by mscorlib:

Assembly mscorlib = typeof(int).Assembly;

var hs = new SortedSet<string>();

foreach (Type type in mscorlib.ExportedTypes)
{
    hs.Add(type.Namespace);
}

foreach (string ns in hs)
{
    Console.WriteLine(ns); 
}

Now... if you want to know which classes/structs are necessary based on the C# specifications 5.0...

By looking for System. I've found these

System.Object, System.ValueType, System.Array, System.Enum, System.Delegate
System.Exception, System.Type, System.Attribute

System.SByte, System.Byte, System.Int16, System.UInt16, System.Int32, System.UInt32, System.Int64, System.UInt64, System.Char, System.Single, System.Double, System.Decimal, System.Boolean, System.Object, System.String 

System.MulticastDelegate (not sure)
System.Void
System.IDisposable 
System.Nullable<T>
System.Linq.Expressions.Expression<D>, 
System.Collections.Generic.IList<T> (implicit cast from System.Array, not sure)
System.Threading.Tasks.Task (for async/await)
System.Threading.Tasks.Task<T> (for async/await)
System.Runtime.CompilerServices.INotifyCompletion (for async/await)
System.Runtime.CompilerServices.ICriticalNotifyCompletion (for async/await)
System.Action (for await, resumption delegate)
System.Collections.IEnumerable (for collection initializer and foreach)
System.Collections.IEnumerable<T> (for foreach)
System.Threading.Monitor (for lock)
System.IntPtr/System.UIntPtr (specific type for volatile fields)

// These attribute are directly recognized by C#
System.AttributeUsageAttribute 
System.Diagnostics.ConditionalAttribute 
System.ObsoleteAttribute 
System.Runtime.CompilerServices.CallerLineNumberAttribute
System.Runtime.CompilerServices.CallerFilePathAttribute
System.Runtime.CompilerServices.CallerMemberNameAttribute 
System.Runtime.CompilerServices.CSharp.IndexerNameAttribute

// These exceptions seems to be "important" for C#
System.OverflowException
System.InvalidOperationException 
System.NullReferenceException 
System.OutOfMemoryException 
System.DivideByZeroException 
System.ArrayTypeMismatchException 
System.ArithmeticException 
System.TypeInitializationException
System.IndexOutOfRangeException
System.StackOverflowException

Note that on top of this there are classes necessary for the dynamic but I haven't found any reference of them in the specifications.

Upvotes: 4

Dimitar Tsonev
Dimitar Tsonev

Reputation: 3892

You're using the csc.exe compiler. All of the predefined types in the base class library are located in the file mscorlib.dll, which is referenced by default when compiling.

So, your questions is what is defined in that dll. One place to start is here:

http://referencesource.microsoft.com/#mscorlib,namespaces

Upvotes: 7

Related Questions