Some helpfull snippets in everyday development for collections, methods, Parallel class, properties and tasks.
Copy files repository or download from Release.
To add snippets to Visual Studio copy files to the origin VS C# snippets directory. For example, in my system this is:
D:\Program Files\Microsoft Visual Studio\2022\Community\VC#\Snippets\1049\Visual C#\
Use by writing name
part without extension from name.snippet
. For example, for list.snippet
write list
and press [Tab] key.
-
list.snippet
- Use ofList{T}
.T
isint
by defaultList<int> list = new List<int>();
-
dict.snippet
- Use ofDictionary{TKey, TValue}
.TKey
isint
,TValue
isstring
by defaultDictionary<int, string> dict = new Dictionary<int, string>();
-
bag.snippet
- Use ofConcurrentBag{T}
.T
isint
by defaultConcurrentBag<int> bag = new ConcurrentBag<int>();
-
method1null.snippet
- method with 1 argument and null-checkingpublic void MyMethod(object arg) { if (arg == null) { throw new ArgumentNullException(nameof(arg)); } // Your code here throw new NotImplementedException(); }
-
method2null.snippet
- method with 2 argument and null-checkingpublic void MyMethod(object arg1, object arg2) { if (arg1 == null) { throw new ArgumentNullException(nameof(arg1)); } if (arg2 == null) { throw new ArgumentNullException(nameof(arg2)); } // Your code here throw new NotImplementedException(); }
-
method3null.snippet
- method with 3 argument and null-checkingpublic void MyMethod(object arg, object arg2, object arg3) { if (arg == null) { throw new ArgumentNullException(nameof(arg)); } if (arg2 == null) { throw new ArgumentNullException(nameof(arg2)); } if (arg3 == null) { throw new ArgumentNullException(nameof(arg3)); } // Your code here throw new NotImplementedException(); }
-
smethod1null.snippet
- static method with 1 argument and null-checkingpublic static void MyMethod(object arg) { if (arg == null) { throw new ArgumentNullException(nameof(arg)); } // Your code here throw new NotImplementedException(); }
-
smethod2null.snippet
- static method with 2 argument and null-checkingpublic static void MyMethod(object arg1, object arg2) { if (arg1 == null) { throw new ArgumentNullException(nameof(arg1)); } if (arg2 == null) { throw new ArgumentNullException(nameof(arg2)); } // Your code here throw new NotImplementedException(); }
-
smethod3null.snippet
- static method with 3 argument and null-checkingpublic static void MyMethod(object arg, object arg2, object arg3) { if (arg == null) { throw new ArgumentNullException(nameof(arg)); } if (arg2 == null) { throw new ArgumentNullException(nameof(arg2)); } if (arg3 == null) { throw new ArgumentNullException(nameof(arg3)); } // Your code here throw new NotImplementedException(); }
-
forp.snippet
- Parallel.For() default constructionParallel.For(0, length, (i, state) => { // Your code here });
-
foreachp.snippet
- Parallel.ForEach() default constructionParallel.ForEach(collection, (item, state) => { // Your code here })
-
foreachpagg.snippet
- Parallel.ForEach() construction with aggregationint aggregatedValue = 0; Parallel.ForEach(collection, () => 0, (item, state, localAgg) => { // Your code here return localAgg; }, (localAgg) => Interlocked.Add(ref aggregatedValue, localAgg) );
-
propd.snippet
- auto-property ofdouble
typepublic double MyProperty { get; set; }
-
propdfull.snippet
- property ofdouble
type with private fieldprivate double myVar; public double MyProperty { get { return myVar; } set { myVar = value; } }
-
propb.snippet
- auto-property ofbool
typepublic bool MyProperty { get; set; }
-
propbfull.snippet
- property ofbool
type with private fieldprivate bool myVar; public bool MyProperty { get { return myVar; } set { myVar = value; } }
-
propnull.snippet
- auto-property ofNullable-type
(defaultdouble
) typepublic double? MyProperty { get; set; }
-
propfullnull.snippet
- property ofNullable-type
(defaultdouble
) type with private fieldprivate double? myVar; public double? MyProperty { get { return myVar; } set { myVar = value; } }
-
taskrun.snippet
- Task.Run constructionTask.Run(() => { // Your code here });
-
taskrunasync.snippet
- Task.Run construction withasync
internal methodTask.Run(async () => { await // Your code here });
-
ataskrun.snippet
- await Task.Run constructionawait Task.Run(() => { // Your code here });