In C# what category does the colon : fall into, and what does it really mean?
Colons are used in a dozen fundamentally different places (that I can think of, with the help of everyone in the comments):
Separating a class name from its base class / interface implementations in class definitions
public class Foo : Bar { }
Specifying a generic type constraint on a generic class or method
public class Foo<T> where T : Bar { }
public void Foo<T>() where T : Bar { }Indicating how to call another constructor on the current class or a base class's constructor prior to the current constructor
public Foo() : base() { }
public Foo(int bar) : this() { }Specifying the global namespace (as C. Lang points out, this is the namespace alias qualifier)
global::System.Console
Specifying attribute targets
[assembly: AssemblyVersion("1.0.0.0")]
Specifying parameter names
Console.WriteLine(value: "Foo");
As part of a ternary expression
var result = foo ? bar : baz;
As part of a
case
orgoto
labelswitch(foo) { case bar: break; }
goto Bar;
Foo: return true;
Bar: return false;Since C# 6, for formatting in interpolated strings
Console.WriteLine($"{DateTime.Now:yyyyMMdd}");
Since C# 7, in tuple element names
var foo = (bar: "a", baz: "b");
Console.WriteLine(foo.bar);
In all these cases, the colon is not used as an operator or a keyword (with the exception of ::
). It falls into the category of simple syntactic symbols, like []
or {}
. They are just there to let the compiler know exactly what the other symbols around them mean.
What does the colon mean?
The ":" means "extends" if you're comparing it to java.
Every class extends object by default.
You need it to extend a class, I'm assuming you already know what extending is, if not feel free to ask.
What colon ( : ) means defining a class in c#?
In this case it means that the HomeController
inherits the Controller
class.
You can read more details about inheritance here, but simply put - inheritance means that everything a Controller
is, a HomeController
is also. A HomeController
is a more finely grained Controller
class.
It can also be used for implementation of interfaces http://msdn.microsoft.com/en-us/library/ms173156.aspx
Question mark and colon in statement. What does it mean?
This is the conditional operator expression.
(condition) ? [true path] : [false path];
For example
string value = someBooleanExpression ? "Alpha" : "Beta";
So if the boolean expression is true, value will hold "Alpha", otherwise, it holds "Beta".
For a common pitfall that people fall into, see this question in the C# tag wiki.
Colon use inside function declaration
Named optional arguments
Its so you can have an optional number of arguments supplied, but the compiler needs to know which arguments to marry up to which parameters, otherwise defaults will be used for the others.
What does the colon sign : do in a SQL query?
That's called a bind variable in Oracle.
what's the name for ":"?
Colon.
What does ':' (colon) do in JavaScript?
var o = {
r: 'some value',
t: 'some other value'
};
is functionally equivalent to
var o = new Object();
o.r = 'some value';
o.t = 'some other value';
What does a colon following a C++ constructor name do?
This is a member initializer list, and is part of the constructor's implementation.
The constructor's signature is:
MyClass();
This means that the constructor can be called with no parameters. This makes it a default constructor, i.e., one which will be called by default when you write MyClass someObject;
.
The part : m_classID(-1), m_userdata(0)
is called member initializer list. It is a way to initialize some fields of your object (all of them, if you want) with values of your choice.
After executing the member initializer list, the constructor body (which happens to be empty in your example) is executed. Inside it you could do more assignments, but once you have entered it all the fields have already been initialized - either to random, unspecified values, or to the ones you chose in your initialization list. This means the assignments you do in the constructor body will not be initializations, but changes of values.
Related Topics
Setting Culture for ASP.NET MVC Application on VS Dev Server and Iis
C# - Outputting Image to Response Output Stream Giving Gdi+ Error
Linq - Query Syntax VS Method Chains & Lambda
How to Store and Retrieve Credentials on Windows Using C#
How to Create a File and Any Folders, If the Folders Don't Exist
Log4Net Logging of Two Different Levels to Two Different Appenders for the Same Logger
Async/Await - Is It *Concurrent*
The JSON Value Could Not Be Converted to System.Int32
Is Graphics.Drawimage Too Slow for Bigger Images
I Have to Access/Commit/Update Svn Repository in Wpf Application Using Svn API or Libraries
Some Help Understanding "Yield"
How to Add Custom Http Header for C# Web Service Client Consuming Axis 1.4 Web Service
How to Implement a Singleton in C#
How to Programmatically Limit Bandwidth Usage of My C# Application
C#: How to Convert a List of Objects to a List of a Single Property of That Object