C#ÖеÄ==,!=µÈ·ûºÅÖØÔØ£¬ÒÔ¼°ÖØÔصȺŲÙ×÷
ͨ¹ýÒ»¸öʵ¼ÊµÄÀý×ÓÀ´½éÉÜ¡£ÆäÖÐÖØÔØ==£¬!=,Equal,GetHashCodeº¯Êý¡£
public class Record
{
public string[] arr = null;
public bool hasEqual = false;
//ÖØÔØÒ»¸öϱêÔËËã·ûºÅ
public string this[int index]
{
get
{
return arr[index];
}
set
{
arr[index] = value;
}
}
public override int GetHashCode()
{
// ÔÚÕâÀïʹÓÃ×Ö·û´®Êý×éµÄhashcode£¬±ÜÃâ×Ô¼ºÍê³ÉÒ»¸öËã·¨
return arr.GetHashCode();
}
public Record(int count)
{
if (count < 1)
{
throw new Exception("Êý×éµÄ³¤¶È²»ÄÜСÓÚ1");
}
arr = new string[count];
}
public static bool operator ==(Record rec1, Record rec2)
{
//×¢ÒâÎÒÃǵ÷ÓÃEqualsÀ´ÅжÏÊÇ·ñÏàµÈ¡£¶ø²»ÊÇÔÚ×Ô¼ºµÄº¯ÊýÖÐÅС¢¶Ï¡£ÕâÊÇÒòΪÈç¹ûÔÚ×Ô¼ºµÄº¯ÊýÖÐÅжϡ£±ÈÈçÓÐrec2=nullµÄÇé¿ö¡£Èç¹ûÊÇÕâÖÖÇé¿ö¡£ÎÒÃÇÒªÅжÏif(rec2==null) {…}¡£ÆäÖÐrec2==nullÒ²Êǵ÷ÓÃÒ»¸öµÈºÅÔËËã·û£¬ÕâÀïÃæÓÐÒ»¸öµÝ¹éµÄ¹ý³Ì£¬Ôì³ÉÁËËÀÑ»·¡£
return Object.Equals(rec1, rec2);
}
public static bool operator !=(Record rec1, Record rec2)
{
return !Object.Equals(rec1, rec2);
}
public override bool Equals(object obj)
{
//ÅжÏÓëÖ®±È½ÏµÄÀàÐÍÊÇ·ñΪnull¡£ÕâÑù²»»áÔì³ÉµÝ¹éµÄÇé¿ö
if (obj == null)
return false;
if (GetType() != obj.GetType())
return false;
Record rec = (Record)obj;
int count = rec.arr.Length;
for (int i = 0; i < count; i++)
{
if (this.arr[i] != rec.arr[i])
{
return false;
}
}
return true;
}
}
ÒÔÉϾÍÊǽéÉÜÖØÔØ==£¬!=,Equal,GetHashCodeµÄ·½·¨¡£µ«ÊÇÓеÄʱºòÎÒÃÇÐèÒªÖØÔØ = ²Ù×÷·û£¬À´·½±ãÎÒÃǽøÐд«µÝ£¬ÕâÔÚC++Àï¿ÉÒÔʹÓ÷ûºÅÖØÔØÀ´ÊµÏÖ£¬µ«ÊÇC#ÀïÃæ¾ÍÐв»Í¨ÁË£¬²»ÐÅÄã¿ÉÒÔÊÔÊÔ£¬±àÒëÆ÷»á±¨´íµÄ¡£C#ÀïʵÏÖÏàͬЧ¹ûʹÓõIJ¢²»ÊÇ¶Ô = ·ûºÅ²Ù×÷½øÐÐÖØÔØ£¬¶øÊÇͨ¹ýÒÔÏ·½·¨À´ÖØÔØ
ÔÀ´C++µÄ³ÌÐòÔ±Ò»¶¨»áдoperator=µÄ,µ«ÊÇÕâÔÚc#ÖÐÊÇ´íÎó
µÄ.
ÎÒÃÇ¿ÉÒÔͨ¹ýת»»À´ÊµÏÖÖØÔصȺŵÄÄ¿µÄ.
c#ÖÐÓÐimplicitºÍexplicit¹Ø¼ü×Ö.¿ÉÒÔʵÏÖÒþʽºÍÏÔʾ
ת»»
ʾÀý:
class MyString
{
private string _str;
private MyString(string str)
{
this._str = str;
}
public static implicit operator MyString(string str)
{
Ïà¹ØÎĵµ£º
ÒÔÏ²ο¼ :http://msdn.microsoft.com/zh-cn/library/87d83y5b.aspx
½Ó¿Ú£¨C# ²Î¿¼£©
¸üУº2007 Äê 11 ÔÂ
½Ó¿ÚÖ»°üº¬·½·¨¡¢ÊôÐÔ¡¢Ê¼þ»òË÷ÒýÆ÷µÄÇ©Ãû¡£³ÉÔ±µÄʵÏÖÊÇÔÚʵÏÖ½Ó¿ÚµÄÀà»ò½á¹¹ÖÐÍê³ÉµÄ£¬ÈçÏÂÃæµÄʾÀýËùʾ£º
ʾÀý
interface ISampleInterface
{
void SampleMethod();
}
class Implem ......
/// <summary>
/// ±È½ÏÁ½·ùͼƬÊÇ·ñÒ»Ö£¨Ê¹ÓÃMarshal.ReadByte·½Ê½£©
/// </summary>
/// <param name="bitmap1">ͼƬ1</param>
/// <param name="bitmap2">ͼƬ2</param>
/// <returns>Èç¹ûÁ½·ùͼƬÏàͬ£¬·µ»Ø0£»Èç¹ûͼƬ1СÓÚͼƬ2£¬·µ»ØСÓÚ0µÄÖµ£»Èç¹ûͼƬ1´óÓÚÍ¼Æ ......
ÔÎĵØÖ·£ºhttp://msdn.microsoft.com/zh-cn/library/79b3xss3(VS.80).aspx
¾²Ì¬ÀàºÍÀà³ÉÔ±ÓÃÓÚ´´½¨ÎÞÐè´´½¨ÀàµÄʵÀý¾ÍÄܹ»·ÃÎʵÄÊý¾ÝºÍº¯Êý¡£¾²Ì¬Àà³ÉÔ±¿ÉÓÃÓÚ·ÖÀë¶ÀÁ¢ÓÚÈκζÔÏó±êʶµÄÊý¾ÝºÍÐÐΪ£ºÎÞÂÛ¶ÔÏó·¢Éúʲô¸ü¸Ä£¬ÕâЩÊý¾ÝºÍº¯Êý¶¼²»»áËæÖ®±ä»¯¡£µ±ÀàÖÐûÓÐÒÀÀµ¶ÔÏó±êʶµÄÊý¾Ý»òÐÐΪʱ£¬ ......
C#»ñÈ¡×Ö·û´®µÄ³¤¶È
×÷Õߣºylclass À´Ô´£º²©¿ÍÔ° ·¢²¼Ê±¼ä£º2009-10-30 16:25 ÔĶÁ£º193 ´Î ÔÎÄÁ´½Ó [ÊÕ²Ø]
1¡¢ ʹÓÃg.MeasureString£¨£©»ñµÃ
ʹÓÃMeasureString²âÁ¿³öÀ´µÄ×Ö·û¿í¶È£¬×ÜÊDZÈʵ¼Ê¿í¶È´óһЩ£¬¶øÇÒËæ×Å×Ö·û ......
µÚÒ»Õ ³ÌÐòÉè¼Æ
¡¡¡¡“ÒªÏë³ÉΪÕæÕýµÄ³ÌÐòÔ±£¬ÎÒÃÇÐèÒª½øÐÐÒ»³¡Ï´Àñ¡£”
¡¡¡¡“³ÌÐò = Êý¾Ý½á¹¹ + Ëã·¨¡£”ÕâÑùµÄ¹«Ê½ºÜ¾«±Ù£¬ËüÔ½¹ýÁ˱í²ãÖ±½ÓÃèÊöÁ˳ÌÐòµÄ±¾ÖÊ¡£²»½öÈç´Ë£¬ÕâÑù¼¸¸ö¼òµ¥µÄµ¥´ÊÒ²ÈÃÎÒÃÇÃ÷°×“ÎÒÃÇÓ¦¸ÃѧϰʲôÄÚÈÝ£¿”¡£ÈËÃÇ×öÈκÎʶ¼ÓÐÒ»¶¨µÄ³ÌÐò£¬Ö»ÊÇûÓÐÒâÊ ......