Home » c# » What is the global keyword in CSharp?

What is the global keyword in CSharp?

During the Christmas break, I received the following question:

What does C# global keyword actually do? Code example, from table adapter code:

[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "")]
public partial class AutoTwitDataSet : global::System.Data.DataSet {

The global keyword tells the compiler to start looking for the namespace or class starting from the root.  You’ll see it in system-generated code so that the code always works.  That way if you have a namespace right under your current namespace that is the same as the top level namespace the code is trying to access, there won’t be a conflict.

For example, say you have namespace A and namespace B and namespace B.A if I write code in namespace B.A that needs to reference a class in namespace A, without global:: I have no way of getting to it.  If I reference A.classname, the compiler will look for classname in B.A.  With global:: I can tell it to look for classname in global::A.classname and it will find classname in the proper location.

This keyword snuck in during version 2.0.  Since most of us don’t need it, most of us don’t even know it exists.  I didn’t until this past weekend.


Other post in c#

Related Post

  • Unsafe Mode in C#Unsafe Mode in C# One of the "advantages" of using CSharp instead of VB.NET is that if programmers want to, they have the option of bypassing the memory management of .NET and working with memory […]
  • Dispose, Finalize and SuppressFinalizeDispose, Finalize and SuppressFinalize I got the following question recently. What is the difference between Dispose and SupressFinalize in garbage collection?” The problem with this question is it assumes Dispose and […]
  • CSharp Numeric OverflowsCSharp Numeric Overflows Did you know that when you are dealing with numbers, by default, .NET will do, or try to do exactly what you tell it to do?  If you tell it to do the impossible, it will do the next […]
  • C# “” better than string.Empty?C# “” better than string.Empty? I recently read an article that argued that “” is “Better than String.Empty” The argument is that since string.Empty doesn’t work in all situations, we should not use it at all.  He […]
  • Just say “No!” to C# Regions?  Really?!Just say “No!” to C# Regions? Really?!   I just read a post by Casademora on “public abstract string[]  Blog()” Just say No! to C# Regions « public abstract string[] Blog() and I still say Regions are not useful… […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer focusing on ASP.NET, C#, Node.js, JavaScript, HTML, CSS, BootStrap, and Angular.JS.Does your team need additional help in any of the above? Contact Dave today.