Programming
Tips & Ticks by SAJJAD
Tips & Ticks by SAJJAD
Oct 30th
[|new] [public|internal|protected|private] [static] [|virtual|abstract|sealed] [|override] Type NAME;
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;
}
}
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;
}
}
}
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;
}
}
}
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 */
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
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
Oct 5th
[MethodImpl(MethodImplOptions.InternalCall), ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)] private static extern void Copy(Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length, bool reliable);
Recent Comments