1. C# / Говнокод #6019

    +116

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    // Method that returns anonymous type as object
    object ReturnAnonymous() {
      return new { City="Prague", Name="Tomas" };
    }
    
    void Main() {
      // Get instance of anonymous type with 'City' and 'Name' properties
      object o = ReturnAnonymous();
    
      // This call to 'Cast' method converts first parameter (object) to the
      // same type as the type of second parameter - which is in this case 
      // anonymous type with 'City' and 'Name' properties
      var typed = Cast(o, new { City="", Name="" });
      Console.WriteLine("{0}, {1}", typed.City, typed.Name)
    }
    
    // Cast method - thanks to type inference when calling methods it 
    // is possible to cast object to type without knowing the type name
    T Cast<T>(object obj, T type) {
      return (T)obj;
    }

    via http://social.msdn.microsoft.com/forums/en-US/linqprojectgeneral/thread/c1c179bb-ea88-4633-970a-947f0dd1e71f/

    Jopa123, 18 Марта 2011

    Комментарии (9)
  2. C# / Говнокод #6010

    +118

    1. 1
    2. 2
    3. 3
    4. 4
    string s=textBox1.Text,d="";
    if (s.lenght > 0)
    for (int i = 0; i < s.lenght- 1; i++)
    d += s[i];

    Код, присланный товарищем мне на проверку. Смех и грех - этот человек учится на третьем курсе политеха на специальности ИТ.

    SpeedyWizard, 17 Марта 2011

    Комментарии (18)
  3. C# / Говнокод #6009

    +128

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    65. 65
    66. 66
    67. 67
    68. 68
    69. 69
    70. 70
    71. 71
    72. 72
    73. 73
    74. 74
    75. 75
    76. 76
    77. 77
    78. 78
    79. 79
    80. 80
    81. 81
    82. 82
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                int x = Convert.ToInt32(comboBox1.Text);
                switch (x)
                {
                    case  2: textBox1.Visible=true;
                        textBox2.Visible = true;
                        textBox3.Visible = false;
                        textBox4.Visible = false;
                        textBox5.Visible = false;
                        textBox6.Visible = false;
                        textBox7.Visible = false;
                        textBox8.Visible = false;
                        textBox9.Visible = false;
                        textBox10.Visible = false;
                        break;
                    case 3:
                        textBox1.Visible = true;
                        textBox2.Visible = true;
                        textBox3.Visible = true;
                        textBox4.Visible = false;
                        textBox5.Visible = false;
                        textBox6.Visible = false;
                        textBox7.Visible = false;
                        textBox8.Visible = false;
                        textBox9.Visible = false;
                        textBox10.Visible = false;
                        break;
    
    //и так до 10
    //...
    
            private void button1_Click(object sender, EventArgs e)
            {
                double p1 = 1 / Convert.ToDouble(textBox1.Text);
                double p2 = 1 / Convert.ToDouble(textBox2.Text);
                double p3 = 1 / Convert.ToDouble(textBox3.Text);
                double p4 = 1 / Convert.ToDouble(textBox4.Text);
                double p5 = 1 / Convert.ToDouble(textBox5.Text);
                double p6 = 1 / Convert.ToDouble(textBox6.Text);
                double p7 = 1 / Convert.ToDouble(textBox7.Text);
                double p8 = 1 / Convert.ToDouble(textBox8.Text);
                double p9 = 1 / Convert.ToDouble(textBox9.Text);
                double p10 = 1 / Convert.ToDouble(textBox10.Text);
                double inv;
                
                
                int x = Convert.ToInt32(comboBox1.Text);
                switch (x)
                {
                    case 2:
                        inv=(1/p1+1/p2);
                        break;
                    case 3:
                        inv = ((1 / p1) + (1 / p2)+(1/p3));
                        break;
                    case 4:
                        inv = ((1 / p1) + (1 / p2) + (1 / p3)+(1/p4));
                        break;
                    case 5:
                        inv = ((1 / p1) + (1 / p2) + (1 / p3) + (1 / p4)+(1/p5));
                        break;
                    case 6:
                        inv = ((1 / p1) + (1 / p2) + (1 / p3) + (1 / p4) + (1 / p5)+(1/p6));
                        break;
                    case 7:
                        inv = ((1 / p1) + (1 / p2) + (1 / p3) + (1 / p4) + (1 / p5) + (1 / p6) + (1 / p7));
                        break;
                    case 8:
                        inv = ((1 / p1) + (1 / p2) + (1 / p3) + (1 / p4) + (1 / p5) + (1 / p6) + (1 / p7) + (1 / p8));
                        break;
                    case 9:
                        inv = ((1 / p1) + (1 / p2) + (1 / p3) + (1 / p4) + (1 / p5) + (1 / p6) + (1 / p7) + (1 / p8) + (1 / p9));
                        break;
                    case 10:
                        inv = ((1 / p1) + (1 / p2) + (1 / p3) + (1 / p4) + (1 / p5) + (1 / p6) + (1 / p7) + (1 / p8) + (1 / p9) + (1 / p10));
                        break;
     
                        double rez = 1 / inv;
                        textBox11.Text = Convert.ToString(rez);
                }
            }

    FMB, 16 Марта 2011

    Комментарии (12)
  4. C# / Говнокод #6002

    +114

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    65. 65
    66. 66
    67. 67
    68. 68
    69. 69
    70. 70
    71. 71
    72. 72
    [Serializable()]
    	public class Vendor
    	{
    		#region Constructors
    
    		public Vendor(long vendorID)
    		{
    			LoadData(vendorID);
    		}
    
    		internal Vendor(Vendor argVendor)
    		{
    			if (argVendor != null)
    			{
    				this.ID = argVendor.ID;
    				this.VendorName = argVendor.VendorName;
    				this.Account = argVendor.VendorAccount;
    				this.EIN = argVendor.VendorEIN;
    				this.Address = argVendor.VendorAddress;
    				this.City = argVendor.VendorCity;
    				this.State = argVendor.VendorState;
    				this.PostalCode = argVendor.VendorZip;
    				this.Phone = argVendor.VendorPhone;
    				this.ContactName = argVendor.VendorContact;
    				this.VendorCode = argVendor.VendorCode;
    				this.Country = argVendor.VendorCountry;
    				this.FaxNumber = argVendor.VendorFax;
    				this.Email = argVendor.Email;
    			}
    		}
    		#endregion
    
    		public void LoadData(long vendorID)
    		{
    			POMRepositoryDataClassesDataContext db = new POMRepositoryDataClassesDataContext();
    			var ven = (from v in db.Vendors
    					   where v.ID == vendorID
    					   select v).SingleOrDefault();
    			if (ven != null)
    			{
                    populateMe(ven);
    			}
    		}
            public void LoadDataByVendorCode(string argVendorCode)
            {
                POMRepositoryDataClassesDataContext db = new POMRepositoryDataClassesDataContext();
                var ven = (from v in db.Vendors
                           where v.VendorCode == argVendorCode
                           select v).Take(1).SingleOrDefault();
                if (ven != null)
                {
                    populateMe(ven);
                }
            }
            private void populateMe(Vendor ven)
            {
                this.ID = ven.ID;
                this.VendorName = ven.VendorName;
                this.Account = ven.VendorAccount;
                this.EIN = ven.VendorEIN;
                this.Address = ven.VendorAddress;
                this.City = ven.VendorCity;
                this.State = ven.VendorState;
                this.PostalCode = ven.VendorZip;
                this.Phone = ven.VendorPhone;
                this.ContactName = ven.VendorContact;
                this.VendorCode = ven.VendorCode;
                this.Country = ven.VendorCountry;
    	    this.FaxNumber = ven.VendorFax;
    	    this.Email = ven.Email;
            }
    }

    No comments!!!

    Arterius, 16 Марта 2011

    Комментарии (4)
  5. C# / Говнокод #5993

    +126

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    Type window = core.getTypeCreationWindowFromType( this.currentType );
                if (window != null)
                {
                    var cr_window = Activator.CreateInstance( window );
                    if (window == typeof( forms.showWindow ))
                    {
                        ((forms.showWindow)cr_window).setTitle( "Добавить новую позицию" );
                        ((forms.showWindow)cr_window).setType( this.chldrenType );
                        ((forms.showWindow)cr_window).fill( );
                        ((forms.showWindow)cr_window).setSelectionMode( );
                        if (this.isExlusivePositionResolve == true) 
                        {
                            // var new_item = Activator.CreateInstance( this.chldrenType );
                            //new_item = (types.ICovertiablePersist<this.chldrenType>)core.instance().ge
    
                            ((forms.showWindow)cr_window).selectedItem += ( forms.showWindow form ) =>
                            {
                                var new_item = Activator.CreateInstance( this.chldrenType );
                                ///new_item = core.instance().getObject(this, form.selected_id);
    
                                int parent_object_id = -1; // Это номер связанного с 
                                                           //выриьбаемым обектом объекта, 
                                                           //тоесть если окно выбора было 
                                                           //кастомизированна и тип выбираемы 
                                                           //х щзначений другой нежели целевой 
                                                           //то мы ищем звязь между обектом ородите и дитя !!!
                                FieldInfo[] fields = this.chldrenType.GetFields( );
                                
                                int id = ((types.persistent)new_item).id;
                                bool isExists = false;
                                for (int i = 0; i < this.dgv_grid.RowCount; i++) 
                                {
                                    int id_s;
                                    int.TryParse( this.dgv_grid.Rows[i].Cells["id"].Value.ToString( ),out id_s );
                                    if (id_s == id) 
                                    {
                                        int count;
                                        int.TryParse( this.dgv_grid.Rows[i].Cells["count"].Value.ToString( ), out count );
                                        this.dgv_grid.Rows[i].Cells["count"].Value = count++;
                                        isExists = true;
                                    }
                                }
                                if (isExists == false) 
                                {
                                    this.addOnePosition( new_item );
                                }
                            };
                        }
                    }
                    else 
                    {
                        ((Form)cr_window).FormClosed += ( object sender, FormClosedEventArgs e ) => {
                            this.fill( );
                        };                   
                    }
                    ((System.Windows.Forms.Form)cr_window).Show( );
                }
                else
                {
                    MessageBox.Show( "Объекты такого типа создавать запрещено" );
                }

    Нашел его миленкого. Переписываю ))) А коменн то комент

    glilya, 15 Марта 2011

    Комментарии (20)
  6. C# / Говнокод #5988

    +140

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    try
    {
    	client.SendData(xml);
    }
    catch(Exception e)
    {
    	string s = e.ToString();
    	if(s.Substring(0,80)=="System.InvalidOperationException: Operation not allowed on non-connected sockets")
    	{
    		client.tcpclient.Close();
    		clients.Remove(client.SessionId);
    	}
    	else if (s.Substring(0,71)=="System.IO.IOException: Unable to write data to the transport connection")
    	{
    		client.tcpclient.Close();
    		clients.Remove(client.SessionId);
    	}
    	else
    	{
    		client.tcpclient.Close();
    		clients.Remove(client.SessionId);
    	}
    }

    нестандартное определение типа исключения
    найдено в примерах кода кандидата на работу

    mozg_raka, 15 Марта 2011

    Комментарии (21)
  7. C# / Говнокод #5985

    +126

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    public class BasisTariffing : IFormattable
    {
    	public override bool Equals(object obj) {
    		BasisTariffing subject = obj as BasisTariffing;
    		if (subject == null) {
    			return false;
    		}
    		return ((Profession == null && subject.Profession == null) || (Profession != null && subject.Profession != null && Profession.Equals(subject.Profession))) &&
    			((BasisRateOf == null && subject.BasisRateOf == null) || (BasisRateOf != null && subject.BasisRateOf != null && BasisRateOf.Equals(subject.BasisRateOf))) &&
    			((BasisRating1Main == null && subject.BasisRating1Main == null) || (BasisRating1Main != null && subject.BasisRating1Main != null && BasisRating1Main.Equals(subject.BasisRating1Main))) &&
    			((BasisRating1Add == null && subject.BasisRating1Add == null) || (BasisRating1Add != null && subject.BasisRating1Add != null && BasisRating1Add.Equals(subject.BasisRating1Add))) &&
    			((BasisRating2Main == null && subject.BasisRating2Main == null) || (BasisRating2Main != null && subject.BasisRating2Main != null && BasisRating2Main.Equals(subject.BasisRating2Main))) &&
    			((BasisRating2Add == null && subject.BasisRating2Add == null) || (BasisRating2Add != null && subject.BasisRating2Add != null && BasisRating2Add.Equals(subject.BasisRating2Add))) &&
    			((BasisRating3Main == null && subject.BasisRating3Main == null) || (BasisRating3Main != null && subject.BasisRating3Main != null && BasisRating3Main.Equals(subject.BasisRating3Main))) &&
    			((BasisRating3Add == null && subject.BasisRating3Add == null) || (BasisRating3Add != null && subject.BasisRating3Add != null && BasisRating3Add.Equals(subject.BasisRating3Add))) &&
    			((BasisRating4Main == null && subject.BasisRating4Main == null) || (BasisRating4Main != null && subject.BasisRating4Main != null && BasisRating4Main.Equals(subject.BasisRating4Main))) &&
    			((BasisRating4Add == null && subject.BasisRating4Add == null) || (BasisRating4Add != null && subject.BasisRating4Add != null && BasisRating4Add.Equals(subject.BasisRating4Add))) &&
    			((BasisRating5Main == null && subject.BasisRating5Main == null) || (BasisRating5Main != null && subject.BasisRating5Main != null && BasisRating5Main.Equals(subject.BasisRating5Main))) &&
    			((BasisRating5Add == null && subject.BasisRating5Add == null) || (BasisRating5Add != null && subject.BasisRating5Add != null && BasisRating5Add.Equals(subject.BasisRating5Add))) &&
    			((BasisRating6Main == null && subject.BasisRating6Main == null) || (BasisRating6Main != null && subject.BasisRating6Main != null && BasisRating6Main.Equals(subject.BasisRating6Main))) &&
    			((BasisRating6Add == null && subject.BasisRating6Add == null) || (BasisRating6Add != null && subject.BasisRating6Add != null && BasisRating6Add.Equals(subject.BasisRating6Add))) &&
    			((BasisRating7Main == null && subject.BasisRating7Main == null) || (BasisRating7Main != null && subject.BasisRating7Main != null && BasisRating7Main.Equals(subject.BasisRating7Main))) &&
    			((BasisRating7Add == null && subject.BasisRating7Add == null) || (BasisRating7Add != null && subject.BasisRating7Add != null && BasisRating7Add.Equals(subject.BasisRating7Add))) &&
    			((BasisRating8Main == null && subject.BasisRating8Main == null) || (BasisRating8Main != null && subject.BasisRating8Main != null && BasisRating8Main.Equals(subject.BasisRating8Main))) &&
    			((BasisRating8Add == null && subject.BasisRating8Add == null) || (BasisRating8Add != null && subject.BasisRating8Add != null && BasisRating8Add.Equals(subject.BasisRating8Add)));
    	}
    
    	public override int GetHashCode() {
    		return ((Profession != null) ? Profession.GetHashCode() : 0) ^
    			((BasisRateOf != null) ? BasisRateOf.GetHashCode() : 0) ^
    			((BasisRating1Main != null) ? BasisRating1Main.GetHashCode() : 0) ^
    			((BasisRating1Add != null) ? BasisRating1Add.GetHashCode() : 0) ^
    			((BasisRating2Main != null) ? BasisRating2Main.GetHashCode() : 0) ^
    			....................................
    	}
    }

    Как вам перегрузочка? И это только часть кода *сущности*, больше просто не вместилось =)

    Guid, 15 Марта 2011

    Комментарии (9)
  8. C# / Говнокод #5975

    +123

    1. 1
    2. 2
    3. 3
    if (!(IsPostBack == true))
    
    // далее везде, см. ссылку

    Реальный индус (Thoothukudi, Tamil Nadu, India) в реальном вопросе на StackOverflow (http://stackoverflow.com/questions/5295149/) наложил натуральную кучу говна.

    abatishchev, 14 Марта 2011

    Комментарии (29)
  9. C# / Говнокод #5895

    +120

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    public class XXX
    {
        private Object m_ForLock = new object();
        private String m_Path = "";
        public XXX(String Path)
        {
            lock (m_ForLock)
            {
                 m_Path = Path;
            }
         }
    }

    javros, 05 Марта 2011

    Комментарии (16)
  10. C# / Говнокод #5884

    +120

    1. 1
    2. 2
    3. 3
    4. 4
    string tmpS;
    //идут всякие монипуляции с этой строкой
    //. . .
    tmpS.Remove(0, tmpS.Length);

    Реальный код из коммерческого продукта.

    inser, 04 Марта 2011

    Комментарии (24)