Shortcut: ntf Available in: C# 2.0+ files where type member declaration or namespace declaration is allowed
[NUnit.Framework.TestFixtureAttribute]
public sealed class $TypeToTest$Tests
{
[NUnit.Framework.TestAttribute]
public void $Test$()
{
var t = new $TypeToTest$()
$END$
}
}
Useful when developing WinForms applications where you want to be sure that code is executing on the correct thread for a given item. Note that Control implements ISynchronizeInvoke.
Useful when developing WinForms applications where a method should be callable from non-UI threads, and that method should then marshall the call onto the UI thread.
Shortcut: inv
Available in: C# 3.0+ files statement is allowed
if (InvokeRequired)
{
Invoke((System.Action)delegate { $METHOD_NAME$($END$); });
return;
}
Macros
METHOD_NAME - Containing type member name
You would normally use this template as the first statement in a given method and the result resembles:
void DoSomething(Type1 arg1)
{
if (InvokeRequired)
{
Invoke((Action)delegate { DoSomething(arg1); });
return;
}
// Rest of method will only execute on the correct thread
// ...
}
Note: This code snippet is dependent on MockRepository (var mocks = new new MockRepository();) being already declared and initialized somewhere else.
using (mocks.Record())
{
$END$
}
using (mocks.Playback())
{
}
*might seem a bit long for a shortcut name but with intellisense not an issue when typing. also have other code snippets for Rhino Mocks so fully qualifying the name makes it easier to group them together visually
This is my favourite because I use it often and it does a lot of work for me.
Shortcut: npc
Available in: C# 2.0+ where expression is allowed.
if (value != _$LOWEREDMEMBER$)
{
_$LOWEREDMEMBER$ = value;
NotifyPropertyChanged("$MEMBER$");
}
Macros:
MEMBER - Containing member type name. Not editable. Note: make sure this one is first in the list.
LOWEREDMEMBER - Value of MEMBER with the first character in lower case. Not editable.
Usage:
Inside a property setter like this:
private string _dateOfBirth;
public string DateOfBirth
{
get { return _dateOfBirth; }
set
{
npc<--tab from here
}
}
It assumes that your backing variable starts with an "_". Replace this with whatever you use. It also assumes that you have a property change method something like this:
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
In reality, the version of this I use is lambda based ('cos I loves my lambdas!) and produces the below. The principles are the same as the above.
public decimal CircuitConductorLive
{
get { return _circuitConductorLive; }
set { Set(x => x.CircuitConductorLive, ref _circuitConductorLive, value); }
}
Create a lambda expression with a different variable declaration for easy nesting.
Shortcut: la, lb, lc
Available in: C# 3.0+ files where expression or query clause is allowed
la is defined as:
x => x.$END$
lb is defined as:
y => y.$END$
lc is defined as:
z => z.$END$
This is similar to Sean Kearon above, except I define multiple lambda live templates for easy nesting of lambdas. "la" is most commonly used, but others are useful when dealing with expressions like this:
Adds virtual keyword. Especially useful when using NHibernate, EF, or similar framework where methods and/or properties must be virtual to enable lazy loading or proxying.
Shortcut: v
Available in: C# 2.0+ file where type member declaration is allowed
virtual $END$
The trick here is the space after virtual, which might be hard to see above. The actual template is "virtual $END$" with reformat code enabled. This allows you to go to the insert point below (denoted by |) and type v:
Neither .NET in general nor the default “equals” template make it easy to bang out a good, simple Equals method. While therearemanythoughts on how to write a good Equals method, I think the following suffices for 90% of simple cases. For anything more complicated — especially when it comes to inheritance — it might be better to not use Equals at all.
Shortcut: equals Available in: C# 2.0+ type members
public override sealed bool Equals(object other) {
return Equals(other as $TYPE$);
}
public bool Equals($TYPE$ other) {
return !ReferenceEquals(other, null) && $END$;
}
public override int GetHashCode() {
// *Always* call Equals.
return 0;
}