What is a structure in C#?
class MyClass <type-parameter-list> : class-base
where <type-parameter-constraints-clauses>
{
// Class body
}
// Example
class MyClass<T> : BaseClass
where T : new()
{
// Class body
}
public SomeGenericClass<some parameters>
where type-parameter : primary-constraint,
secondary-constraints,
constructor-constraint
// Example
public class MyClass<T>
where T: class, IEnumerable<T>, new()
{…}
public static T Min<T>(T first, T second)
where T : IComparable<T>
{
if (first.CompareTo(second) <= 0)
{
return first;
}
else
{
return second;
}
}
System.IO.StreamReader reader = System.IO.File.OpenText("file.txt");
using IO = System.IO;
using WinForms = System.Windows.Forms;
IO.StreamReader reader = IO.File.OpenText("file.txt");
WinForms.Form form = new WinForms.Form();
Indexers enable objects to be indexed in a similar manner to arrays.
A get accessor returns a value.
A set accessor assigns a value.
The this keyword is used to define the indexer.
The value keyword is used to define the value being assigned by the set indexer.
Indexers do not have to be indexed by an integer value; it is up to you how to define the specific look-up mechanism.
Indexers can be overloaded.
Indexers can have more than one formal parameter, for example, when accessing a two-dimensional array.
IndexedType t = new IndexedType(50);
int i = t[5];
t[0] = 42;
personInfo["John Doe", 25]
public static Matrix operator *(Matrix m1, Matrix m2)
{
return new m1.Multiply(m2);
}
Operators | Overloadability |
---|---|
+, -, !, ~, ++, --, true, false | These unary operators can be overloaded. |
+, -, *, /, %, &, |, ^, <<, >> | These binary operators can be overloaded. |
==, !=, <, >, <=, >= | The comparison operators can be overloaded (but see the note that follows this table). |
&&, || | The conditional logical operators cannot be overloaded, but they are evaluated using & and |, which can be overloaded. |
[] | The array indexing operator cannot be overloaded, but you can define indexers. |
(T)x | The cast operator cannot be overloaded, but you can define new conversion operators (see explicit and implicit). |
+=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>= | Assignment operators cannot be overloaded, but +=, for example, is evaluated using +, which can be overloaded. |
=, ., ?:, ??, ->, =>, f(x), as, checked, unchecked, default, delegate, is, new, sizeof, typeof | These operators cannot be overloaded. |
NOTES: The comparison operators, if overloaded, must be overloaded in pairs; that is, if == is overloaded, != must also be overloaded. The reverse is also true, and similar for < and >, and for <= and >=.