Oldalak

2013. február 4., hétfő

C# adattípusok táblázata

C# adattípus .NET Mérete Legkisebb érték Legnagyobb érték
sbyte System.Sbyte 1 -128 127
byte System.Byte 1 0 255
short System.Int16 2 -32 768 32 767
ushort System.UInt16 2 0 65 535
int System.Int32 4 -2 147 483 648 2 147 483 647
uint System.UInt32 4 0 4 294 967 295
long System.Int64 8 -9 223 372 036 854 775 808 9 223 372 036 854 775 807
ulong System.UInt64 8 0 18 446 744 073 709 551 615
char System.Char 2 0 65 535
float System.Single 4 1,5*10-45 3,4*1038
double System.Double 8 5,0*10-324 1,7*10308
bool System.Boolean 1 false (0) true (1)
decimal System.Decimal 16 1,0*10-28 kb. 7,9*1028

C# összetett aritmetikai értékadó műveletek

Műveleti jel Használati alak Kifejezése Művelet
+= x += 4 x = x + 4

összeadás

-=

x -= 4 x = x - 4

kivonás

*= x *= 4 x = x * 4

szorzás

/= x /= 4 x = x / 4

osztás

%= x %= 4 x = x % 4 maradék képzés (modulo)

Thread-safe TCounter osztály

interface

type
  TCounter = class
  public
     constructor Create;
     function GetCounter : Integer;
  end;

var
  Counter : Integer;
  CriticalSection : TRtlCriticalSection;

implementation

constructor TCounter.Create;
begin

  inherited;
  EnterCriticalSection(CriticalSection);
  try
    Counter := Counter + 1;
  finally
    LeaveCriticalSection(CriticalSection);
  end;
end;

function TCounter.GetCounter : Integer;
begin
  // nem kell kritikus tartomány, mert az Integer
  // elemi változó
  Result := Counter;
end;

initialization
  InitializeCriticalSection(CriticalSection);
finalization
  DeleteCriticalSection(CriticalSection);
end.

Provider pattern

public static class Note 
{
  private static NoteProvider provider; 
  static Note() 
  {
    string providerName = ConfigurationManager.AppSettings["noteProvider"]; 
    Type providerType = Type.GetType(providerName); 
    provider = (NoteProvider)Activator.CreateInstance(providerType); 
  }
  public static string GetNote() 
  {
    return provider.GetNote(); 
  }
}
 
public abstract class NoteProvider 
{
  public abstract string GetNote(); 
}
 
public class FirstNoteProvider : NoteProvider 
{
  public override string GetNote() 
  {
    return "első note"; 
  }
}
 
  
public class SecondNoteProvider : NoteProvider
{
  public override string GetNote() 
  {
    return "második note"; 
  }
}

Thread safe Singleton minta

class Program 
{
  static void Main(string[] args)
  {
  Singleton s, s2;
  s = Singleton.GetInstance();
  s2 = Singleton.GetInstance();
 
  // a két objektum referenciája egyenlő lesz
  Console.WriteLine(ReferenceEquals(s,s2));
  }
}
 
class Singleton
{
  private static Singleton instance; 
 
  /// statikus konstruktor
  /// a legelső osztáylra történő hivatkozákor lefut
  static Singleton()
  {
  instance = new Singleton();
  }
 
  public static Singleton GetInstance()
  {
  return instance;
  }
}

Műveletek precedenciája

Szint Művelettípus Műveletek
1 Elsődleges () . [] x++ x-- new typeof sizeof checked unchecked
2 Egyváltozós + - ! ~ ++x --x
3 Multiplikatív * / %
4 Additív + -
5 Eltolási << >>
6 Viszonító < > <= >= is
7 Egyenlőségi == !=
8 Logikai AND &
9 Logikai XOR ^
10 Logikai OR |
11 Feltételes AND &&
12 Feltételes OR ||
13 Feltételes művelet ?:
14 Értékadás = *= /= %= += -= <<= >>= &= ^= |=

Domain MX rekordjának lekérdezése

nslookup -q=mx domain.name

A domain.name helyére azt a nevet kell megadni, amiről az MX információt le szeretnénk kérdezni.