-
-
Save caviles/bbdb3938af7312bfb7cbf7630dffe4f7 to your computer and use it in GitHub Desktop.
Change the hover over object value in the debug window https://msdn.microsoft.com/en-us/library/x810d419.aspx | |
null coelescing opertor | |
string i = name ?? "no name"; these is a shorted version of the ternary ? name : "no name" | |
//deep checks and = | |
string i = name ?? lname ?? fname ?? "no name"; these is a shorted version of the ternary ? name : "no name" | |
don't call virtual method from a constructor because someone may override it not knowing that it should be implemented a certain way | |
//find out who called us | |
public static void ShowCallerInfo([CallerMemberName] | |
string callerName = null, [CallerFilePath] string | |
callerFilePath = null, [CallerLineNumber] int callerLine=-1) | |
{ | |
Console.WriteLine("Caller Name: {0}", callerName); | |
Console.WriteLine("Caller FilePath: {0}", callerFilePath); | |
Console.WriteLine("Caller Line number: {0}", callerLine); | |
} | |
A partial method has its signature defined in one part of a partial type, and its implementation defined in another part of the type. Partial methods enable class designers to provide method hooks, similar to event handlers, that developers may decide to implement or not. If the developer does not supply an implementation, the compiler removes the signature at compile time. The following conditions apply to partial methods: | |
Signatures in both parts of the partial type must match. | |
The method must return void. | |
No access modifiers are allowed. Partial methods are implicitly private. | |
The following example shows a partial method defined in two parts of a partial class: | |
C# | |
namespace PM | |
{ | |
partial class A | |
{ | |
partial void OnSomethingHappened(string s); | |
} | |
// This part can be in a separate file. | |
partial class A | |
{ | |
// Comment out this method and the program | |
// will still compile. | |
partial void OnSomethingHappened(String s) | |
{ | |
Console.WriteLine("Something happened: {0}", s); | |
} | |
} | |
} | |
//allow internal classes to be visble by some specific assemblies thiis must be done in assemblyinfo.cs | |
InternalsVisibleToAttribute Class | |
.NET Framework (current version) Other Versions | |
Specifies that types that are ordinarily visible only within the current assembly are visible to a specified assembly. | |
always use seed value with random or it may produce the same number when it runs against the sytem clock | |
[Flags] allows a bitwise version of enum so 3 will be equal to 3 and enum 1 and 2 | |
.tolost will prevent reeavl | |
Array.BinarySearch will return the index of the value | |
clone creates a shallow copy so they point to the same place in memory a deep copy would givee you a new object | |
public Person ShallowCopy() | |
{ | |
return (Person)this.MemberwiseClone(); | |
} | |
public Person DeepCopy() | |
{ | |
Person other = (Person)this.MemberwiseClone(); | |
other.IdInfo = new IdInfo(this.IdInfo.IdNumber); | |
other.Name = String.Copy(this.Name); | |
return other; | |
} | |
when parsing use number styles it has a lot of enumbers | |
decimal x = decimal.Parse("1.2345E-02", NumberStyles.Float); | |
Console.WriteLine(x); // Prints 0.012345 | |
datetime.pparseextact allows us to set the format to avoid culture issues | |
with as operator always do a null check | |
use cast when we are certian thetypes are the same else use as and null check | |
use yeild and return ienumberable instead of creating a collection to return | |
//alaias namespaces | |
using colAlias = System.Collections; | |
namespace System | |
{ | |
class TestClass | |
{ | |
static void Main() | |
{ | |
// Searching the alias: | |
colAlias::Hashtable test = new colAlias::Hashtable(); | |
creating in indexer on you objects | |
class TempRecord | |
{ | |
// Array of temperature values | |
private float[] temps = new float[10] { 56.2F, 56.7F, 56.5F, 56.9F, 58.8F, | |
61.3F, 65.9F, 62.1F, 59.2F, 57.5F }; | |
// To enable client code to validate input | |
// when accessing your indexer. | |
public int Length | |
{ | |
get { return temps.Length; } | |
} | |
// Indexer declaration. | |
// If index is out of range, the temps array will throw the exception. | |
public float this[int index] | |
{ | |
get | |
{ | |
return temps[index]; | |
} | |
set | |
{ | |
temps[index] = value; | |
} | |
} | |
} |
use & if you truely want to check both sides of a condition instead of the short circuited &&
The following code example demonstrates how to use the Zip<TFirst, TSecond, TResult> method to merge two sequences.
C#VB
int[] numbers = { 1, 2, 3, 4 };
string[] words = { "one", "two", "three" };
var numbersAndWords = numbers.Zip(words, (first, second) => first + " " + second);
foreach (var item in numbersAndWords)
Console.WriteLine(item);
// This code produces the following output:
// 1 one
// 2 two
// 3 three
if you want to hide interface implementations you must make them expliciitly
Task.Delay to input a delay time
use sort generic collection such as sortedset and sorteddictionary to ensure order is sorted
sets don't allow dups
lazy differs initializatin until it is used
private Lazy _myProperty = new Lazy( () => new MyClass());
public MyClass MyProperty
{
get
{
return _myProperty.Value;
}
}
[DebuggerStepThrough] Attribute can be used to skip areas during debugging
to use reserved keywords use the @namespace symbol
be careful with static constructors if they throw an exception you will not be use your class for the remainder of the program