Tips & Ticks by SAJJAD
C#
C# implicit operator
Aug 20th
public class StringSafe
{
/// < summary>
/// Empty value
/// < /summary>
public const string Empty = "";
/// < summary>
/// String to StringSafe
/// < /summary>
/// < param name="data">object< /param>
/// < returns>< /returns>
public static implicit operator StringSafe(String data)
{
if (data == null)
return Empty;
return new StringSafe(data);
}
/// < summary>
/// StringSafe to String
/// < /summary>
/// < param name="data">object< /param>
/// < returns>< /returns>
public static implicit operator String(StringSafe data)
{
if (data == null)
return Empty;
return data.value;
}
private String value = Empty;
/// < summary>
/// Value
/// < /summary>
public String Value
{
get
{
if (this.value == null)
return Empty;
return this.value;
}
set
{
if (value == null)
this.value = Empty;
else
this.value = value;
}
}
/// < summary>
/// Create new instance of StringSafe
/// < /summary>
/// < param name="value">< /param>
public StringSafe(String value = Empty)
{
this.value = value;
}
}
Sample
public class Program
{
StringSafe sf = "SAJJAD";
MessageBox.Show(sf);
}
Anonymous Struct
Aug 18th
var Freinds = new[]
{
new { Name = "Ali", Age = 23 , Married = false },
new { Name = "Reza", Age = 43 , Married = true },
new { Name = "Ehsan", Age = 36 , Married = true },
new { Name = "Mahdi", Age = 34 , Married = false }
};
String Output = String.Empty;
foreach (var v in Freinds.Where(a => a.Married))
Output += "Name: " + v.Name + ", Age:" + v.Age + ";";
if (Output.Length > 0)
MessageBox.Show(Output.Substring(0, Output.Length - 1));
C# Define Field, Property, Method or Class
Oct 30th
[|new] [public|internal|protected|private] [static] [|virtual|abstract|sealed] [|override] Type NAME;
C# partial Keyword
Oct 25th
using System;
namespace ConsoleApplication
{
static class Program
{
static void Main()
{
User a = new User();
Console.WriteLine(User.Instance);
User b = new User();
Console.WriteLine(User.Instance);
Console.Read();
}
}
public enum Gender
{
Male,
Female
}
public partial class User
{
private Gender _gender;
public Gender Gender
{
get
{
return this._gender;
}
set
{
this._gender = value;
}
}
public User()
{
Instance++;
}
~User()
{
Instance--;
}
}
public partial class User
{
public static int Instance = 0;
public static readonly User Empty;
}
}
C# virtual Keywords: More speed
Oct 14th
using System;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Area of 5 is: " + (new Circle(5).Area()).ToString());
Console.WriteLine("Perimeter of 5 is: " + (new Circle(5).Perimeter()).ToString());
Circle C = new Circle(8.5);
Console.WriteLine("\nArea of 8.5 is: " + C.Area().ToString());
Console.WriteLine("Perimeter of 8.5 is: " + C.Perimeter().ToString());
Console.Read();
}
}
abstract class Shape
{
public double X, Y;
public Shape() : this(0) { }
public Shape(double X)
{
this.X = this.Y = X;
}
public Shape(double X, double Y)
{
this.X = X;
this.Y = Y;
}
public virtual double Area()
{
return this.X * this.Y;
}
public virtual double Perimeter()
{
return 2 * this.X * this.Y;
}
}
sealed class Circle : Shape
{
public Circle() : base(0) { }
public Circle(double Radius) : base(Radius) { }
public override double Area()
{
return Math.PI * this.X * this.X;
}
public override double Perimeter()
{
return 2 * Math.PI * this.X;
}
}
}
C# params Keywords
Oct 14th
using System;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
if (Containt(typeof(int), new object[] { true, 1, 110.1F, 20L, 80.0001D, "SAJJAD", DateTime.Now }))
{
Console.WriteLine("Array contain Int32 type !");
}
if (Containt(typeof(DateTime), true, 1, 110.1F, 20L, 80.0001D, "SAJJAD", DateTime.Now))
{
Console.WriteLine("Array contain Datetime type !");
}
Console.ReadLine();
}
static bool Containt(Type type, params object[] list)
{
foreach (object obj in list)
if (obj.GetType() == type)
return true;
return false;
}
}
}
Abstract, Sealed & Static modifiers in C#
Oct 10th
C# provides many modifiers for use with types and type members. Of these, three can be used with classes: abstract, sealed and static.
abstract
The abstract modifier indicates that the thing being modified has a missing or incomplete implementation. The abstract modifier can be used with classes, methods, properties, indexers, and events. Use the abstract modifier in a class declaration to indicate that a class is intended only to be a base class of other classes. Members marked as abstract, or included in an abstract class, must be implemented by classes that derive from the abstract class
abstract class A {}
A a = new A(); //Error
sealed
When applied to a class, the sealed modifier prevents other classes from inheriting from it. In the following example, class B inherits from class A, but no class can inherit from class B.
sealed class A {}
class B:A {} //Error
static
The static modifier can be used with classes, fields, methods, properties, operators, events, and constructors, but it cannot be used with indexers, destructors, or types other than classes. The static modifier on a class means that the class cannot be instantiated, and that all of its members are static. A static member has one version regardless of how many instances of its enclosing type are created
static class A {}
A a = new A(); //Error
/* All member must be static */
C# Reserved and Contextual Keywords
Oct 8th
Specification keywords
abstract | as | base | bool | break | byte | case | catch | char | checked | class | const | continue | decimal | default | delegate | do | double | else | enum | event | explicit | extern | false | finally | fixed | float | for | foreach | goto | if | implicit | in | int | interface | internal | is | lock | long | namespace | new | null | object | operator | out | override | params | private | protected | public | readonly | ref | return | sbyte | sealed | short | sizeof | stackalloc | static | string | struct | switch | this | throw | true | try | typeof | uint | ulong | unchecked | unsafe | ushort | using | virtual | void | volatile | while
Magic keywords
__arglist | __makeref | __reftype | __refvalue
Keywords as an identifier
@typeof @goto = @for.@switch(@throw);
Preprocessor keywords
#define | hidden | default | disable | restore | checksum
C# 1.0 Contextual keywords
get | set | value | add | remove
C# 2.0 Contextual keywords
where | partial | global | yield | alias
C# 3.0 Contextual keywords
from | join | on | equals | into | orderby | ascending | descending | group | by | select | let | var
C# 4.0 Contextual keywords
dynamic
C# speed: ‘return’ vs ‘yield return’
Oct 7th
Declare;
public static T[] Reserve< T>(T[] array)
{
T[] newArray = new T[array.Length];
for (i = 0; i < array.Length; i++)
newArray[i] = array[array.Length - 1 - i];
return newArray;
}
public static IEnumerable ReserveAsync< T>(T[] array)
{
for (i = 0; i < array.Length; i++)
yield return array[array.Length - 1 - i];
}
Use;
int[] ii = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
long a;
a = DateTime.Now.Ticks;
foreach (int i in xArray.Reserve(ii))
Debug.WriteLine(i);
Debug.WriteLine(DateTime.Now.Ticks - a);
a = DateTime.Now.Ticks;
foreach (int i in xArray.ReserveAsync(ii))
Debug.WriteLine(i);
Debug.WriteLine(DateTime.Now.Ticks - a);
Result;
ReserveAsync often is 60% faster than Reserve
Recent Comments