yield
by Michael Stum Used in an iterator block to provide the value of an enumerator object or to report an end of iterationyield return <expression>; yield break ;
var
by Michael Stum . Declares a variablevar index;
using()
statement by kokos. Provides convenient syntax to ensure proper use of IDisposable objects.using (System.IO.StreamReader sr = new System.IO.StreamReader(@"C:\Users\Public\Documents\test.txt"))
{
string s = null;
while((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
readonly
by kokos . The readonly keyword is a modifier that can be used for fields. If the field declaration contains the readonly modifier, the assignment of values ​​to such fields can only occur as part of the declaration or in a constructor in the same class.class Age
{
readonly int _year;
Age(int year)
{
_year = year;
}
void ChangeYear()
{
//_year = 1967; // Compile error if uncommented.
}
}
as
by Mike Stoneas
/ is
by Ed Swangrenas
/ is
(improved) by Rocketpantsdefault
by deathofratsglobal::
by pzycoman . When the context-sensitive global keyword precedes the operator , it refers to the global namespace, which is the default namespace for any C # program and is not otherwise named.class TestClass : global::TestApp { }
volatile
by Jakub Ĺ turc . The volatile keyword indicates that a field can be edited by several threads running simultaneously. Fields declared as volatile are not optimized by the compiler, which provides access through a separate stream. This ensures that the field has the most current value at any time.class VolatileTest
{
public volatile int i;
public void Test (int _i)
{
i = _i;
}
}
extern alias
by Jakub Ĺ turc . In some cases, it may be necessary to provide references to two versions of assemblies with the same complete type names. For example, you need to use two or more versions of an assembly in the same application. Using an external assembly alias, you can include namespaces for each assembly in a wrapper within the root-level namespaces named for this alias, which allows them to be used in a single file.DefaultValue
by Michael Stum. Attribute to set the default value of the parameter.ObsoleteAttribute
by DannySmurf. Marks program elements that are no longer used.DebuggerDisplayAttribute
by Stu . Determines how a class or field is displayed in the debugger variable windows.DebuggerBrowsable
. Specifies the presence and method of displaying members in the debugger variable windows.DebuggerStepThrough
by bdukes . Specifies the DebuggerStepThroughAttribute class. This class cannot be inherited.ThreadStaticAttribute
by marxidad. Indicates that the value of a static field is unique for each thread.FlagsAttribute
by Martin Clarke. Indicates that an enumeration can be processed as a bitfield, which is a set of flags.[SerializableAttribute]
[AttributeUsageAttribute(AttributeTargets.Enum, Inherited = false)]
[ComVisibleAttribute(true)]
public class FlagsAttribute : Attribute
ConditionalAttribute
by AndrewBurns??
operator by kokos. Operator ?? is called an operator that supports the value NULL, and is used to determine the default value for zero value types, as well as for reference types. It returns the left operand if it is not NULL, otherwise it returns the right operand.class NullCoalesce
{
static int? GetNullableInt()
{
return null;
}
static string GetStringValue ()
{
return null;
}
static void Main ()
{
// ?? operator example.
int? x = null;
// y = x, unless x is null, in which case y = -1.
int y = x ?? -one;
// Assign i to return value of method, unless
// return value is null, in which case assign
// default value of int to i.
int i = GetNullableInt () ?? default (int);
string s = GetStringValue ();
// ?? also works with reference types.
// Display contents of s, unless s is null,
// in which case display "Unspecified".
Console.WriteLine (s ?? "Unspecified");
}
}
Decimal = M
Float = F
Double = D
// for example
double d = 30D;
x => x.ToString() //simplify so many calls
Public int MyId { get; private set; }
using web = System.Web.UI.WebControls;
using win = System.Windows.Forms;
web :: Control aWebControl = new web :: Control ();
win :: Control aFormControl = new win :: Control ();
"c:\\program files\\oldway"
@"c:\program file\newway"
enum
values ​​by lfoust .Enum need not be int.public enum MyEnum : long
{
Val1 = 1,
Val2 = 2
}
MyEnum e = (MyEnum)123;
event
operators by marxidad. Use the event keyword to announce an event in the publisher class.public class SampleEventArgs
{
public SampleEventArgs(string s) { Text = s; }
public String Text {get; private set;} // readonly
}
public class Publisher
{
// Declare the delegate (if using non-generic pattern).
public delegate void SampleEventHandler(object sender, SampleEventArgs e);
// Declare the event.
public event SampleEventHandler SampleEvent;
// Wrap the event in a protected virtual method
// to enable the event.
protected virtual void RaiseSampleEvent ()
{
// Raise the event by using the () operator.
SampleEvent (this, new SampleEventArgs ("Hello"));
}
}
:?
) by JasonS . condition? first_expression: second_expression;checked
and unchecked
operator by Binoj Antonyvar v = new { Amount = 108, Message = "Hello" };
__makeref __reftype __refvalue
by Judah HimangoPerson p = new Person() {FirstName="John",LastName="Doe",Phone="602-123-1234",City="Phoenix"};
class ExtensionMethods2
{
static void Main ()
{
int [] ints = {10, 45, 15, 39, 21, 26};
var result = ints. OrderBy (g => g);
foreach (var i in result)
{
System.Console.Write (i + "");
}
}
}
// Output: 10 15 21 26 39 45
public partial class Employee
{
public void DoWork()
{
}
}
public partial class Employee
{
public void GoToLunch ()
{
}
}
DEBUG
pre-processor directive by Robert Durgin #if debug-code defined in such a block will be executed only during debag. It is convenient to make more detailed conclusions of exceptions when developing.TransactionScope
by KiwiBastard. Makes the code block transactional. This class cannot be inherited.Mutex
by Mutex
System.IO.Path
by ageektrapped. Performs operations on instances of the String class that contain the path information of a file or directory. These operations are performed in a cross-platform way.WeakReference
by Juan Manuel . WeakReference
weak link that points to an object, but allows you to delete it to the garbage collector.String.IsNullOrEmpty()
method by KiwiBastard. Indicates whether the specified String object is null (Nothing in Visual Basic) or the string is Empty .List.ForEach()
method by KiwiBastardusing System;
using System.Collections.Generic;
class program
{
static void Main ()
{
List <String> names = new List <String> ();
names.Add ("Bruce");
names.Add ("Alfred");
names.Add ("Tim");
names.Add ("Richard");
// Display the contents of the List out using the "Print" delegate.
names.ForEach (Print);
// The following demonstrates the Anonymous Delegate feature of C #
// to display the contents of the list.
names.ForEach (delegate (String name)
{
Console.WriteLine (name);
});
}
private static void Print (string s)
{
Console.WriteLine (s);
}
}
/ * This code will produce output to the following:
* Bruce
* Alfred
* Tim
* Richard
* Bruce
* Alfred
* Tim
* Richard
* /
BeginInvoke()
, EndInvoke()
methods by Will DeanNullable<T>.HasValue
and Nullable<T>. Value
Nullable<T>. Value
properties by RismoGetValueOrDefault
method by John Sheehan. Returns the value of the current Nullable (T) object or the specified default value.Source: https://habr.com/ru/post/56279/
All Articles