What does the '= ' syntax in C# mean?
It's the lambda operator.
From C# 3 to C# 5, this was only used for lambda expressions. These are basically a shorter form of the anonymous methods introduced in C# 2, but can also be converted into expression trees.
As an example:
Func<Person, string> nameProjection = p => p.Name;
is equivalent to:
Func<Person, string> nameProjection = delegate (Person p) { return p.Name; };
In both cases you're creating a delegate with a Person
parameter, returning that person's name (as a string).
In C# 6 the same syntax is used for expression-bodied members, e.g.
// Expression-bodied property
public int IsValid => name != null && id != -1;
// Expression-bodied method
public int GetHashCode() => id.GetHashCode();
See also:
- What's the difference between anonymous methods (C# 2.0) and lambda expressions (C# 3.0)
- What is a Lambda?
- C# Lambda expression, why should I use this?
(And indeed many similar questions - try the lambda and lambda-expressions tags.)
What does 'is { }' syntax mean in c#?
In the more general sense, this is member-based pattern matching - for example:
if (foo is { Id: 42, Name: "abc"})
{
}
tests whether foo
has Id
42 and Name
"abc". In this case, you are testing zero properties, so it effectively becomes the same as is object
(i.e. a not null
test, or a no-op true
for non-nullable value-types).
To compare against what you have in the question (if (someObj == null)
) - it is the opposite of that, noting that it will also not use an overloaded ==
operator for the null
test (which == null
will).
What does C# syntax ;; mean?
;
means empty statement. It does nothing.
In some cases (for example robotic programming), it is useful to instruct the program to continuously pool environment condition and do nothing until the condition fulfilled.
Example
// turn motor on so robot is going forward
TurnMotorOn();
// Do nothing (means motor is still on and robot is still going forward) until there is obstacle in front of the robot.
while(!ThereIsObstacle()) ;
// turn motor off so robot is stopped.
TurnMotorOff();
What is the @ syntax in C#
The prefix "@" enables the use of keywords as identifiers, which is
useful when interfacing with other programming languages. The
character @ is not actually part of the identifier, so the identifier
might be seen in other languages as a normal identifier, without the
prefix. An identifier with an @ prefix is called a verbatim
identifier.
Example:
class @class
{
public static void @static(bool @bool) {
if (@bool)
System.Console.WriteLine("true");
else
System.Console.WriteLine("false");
}
}
See: http://msdn.microsoft.com/en-us/library/aa664670.aspx
What does T represent in C# syntax?
The remaining bits -
Execute<T> ... where T : new()
is strange and new to me.
So, Execute
is method name. <T>
is generic parameter T
(See Generics (C# Programming Guide)) and where T : new()
is generic constraint, which requires T
to have parameterless constructor.
Possibly related, but what does the following actually return? (as in; does it return a function or an object or a reference to something?)
return Execute<Call>(request);
Because Execute<T>
returns T
and you call is with T = Call
this one will return Call
class instance (or possible null
if only Call
is class
, not struct
).
what does this syntax int* mean?
int*
declares pointer to int
type. C# supports pointer in unsafe context. We can use the unsafe keyword in two different ways. It can be used as a modifier to a method, property, and constructor etc. For example:
using System;
class MyClass
{
public unsafe void Method()
{
int x = 10;
int y = 20;
int *ptr1 = &x;
int *ptr2 = &y;
Console.WriteLine((int)ptr1);
Console.WriteLine((int)ptr2);
Console.WriteLine(*ptr1);
Console.WriteLine(*ptr2);
}
}
class MyClient
{
public static void Main()
{
MyClass mc = new MyClass();
mc.Method();
}
}
The keyword unsafe can also be used to mark a group of statements as unsafe as shown below:
using System;
class MyClass
{
public void Method()
{
unsafe
{
int x = 10;
int y = 20;
int *ptr1 = &x;
int *ptr2 = &y;
Console.WriteLine((int)ptr1);
Console.WriteLine((int)ptr2);
Console.WriteLine(*ptr1);
Console.WriteLine(*ptr2);
}
}
}
class MyClient
{
public static void Main()
{
MyClass mc = new MyClass();
mc.Method();
}
}
Reference: Pointers in C#
what does this syntax mean? new { }
This is an anonymous type. It will basically work like a class:
class anonymous
{
public readonly type var1; // "type" is the type of somevalue
public readonly type var2; // "type" is the type of anothervalue
}
which is instantiated with
var data = new anonymous { var1 = somevalue, var2 = anothervalue };
Related Topics
Using Excel Oledb to Get Sheet Names in Sheet Order
Servicestack Request Dto Design
Round a Double to X Significant Figures
C# If/Then Directives for Debug VS Release
Converting a .Net Func<T> to a .Net Expression<Func<T>>
Pass Table Valued Parameter Using Ado.Net
What Is Cool About Generics, Why Use Them
Replace "\\" with "\" in a String in C#
Cursor.Current VS. This.Cursor
Changing the User Agent of the Webbrowser Control
How to Securely Save Username/Password (Local)
How to Implement a Configurationsection with a Configurationelementcollection