تمرین c# کلاس گروه A مهندس شکری
انواع متدهای های زیر را تحقیق کنید و جالت های مختلف چگونه است ؟
string.compare (A,B) { >0 => A>B
0= A=B
<0 A<> <>
جواب :
public static int Compare( string strA, string strB, bool ignoreCase )
Parameters
- strA
- Type: System.String
The first string to compare.
- strB
- Type: System.String
The second string to compare.
- ignoreCase
- Type: System.Boolean
true to ignore case during the comparison; otherwise, false.
Return Value
Type: System.Int32
A 32-bit signed integer that indicates the lexical relationship between the two comparands.
A 32-bit signed integer that indicates the lexical relationship between the two comparands.
Value | Condition |
|---|---|
Less than zero | strA is less than strB. |
Zero | strA equals strB. |
Greater than zero | strA is greater than strB. |
--------------------
string testString = "Test"; string anotherString = "Another"; if (testString.CompareTo(anotherString) == 0) {} if (testString.Equals(anotherString)) {} if (testString == anotherStirng) {}
---------------------------------public static int Compare(string strA, string strB) { return CultureInfo.CurrentCulture.CompareInfo.Compare(strA, strB, CompareOptions.None); } public int CompareTo(string strB) { if (strB == null) { return 1; } return CultureInfo.CurrentCulture.CompareInfo.Compare(this, strB, CompareOptions.None); }
----------------------------------
string s1 = "String to compare.";string s2 = "String to compare.";string s3 = "String to Compare."; // Note the capital 'C'bool result;result = s1 == s2; // result = trueresult = s1 == s3; // result = falseresult = s1 != s2; // result = falseresult = s1 != s3; // result = true| Return Value | Meaning |
|---|---|
| Zero | The strings are equal. |
| Less than Zero | The first string is less than the second. |
| More than Zero | The first is greater than the second or the second string is null. |
This can be clarified with an example:
string animal1 = "Cat";string animal2 = "Dog";int result;result = animal1.CompareTo("Cat"); // result is zeroresult = animal2.CompareTo("Cat"); // result is greater than zeroresult = animal1.CompareTo(animal2); // result is less than zerostring animal = null;int result;result = animal.CompareTo("Cat"); // Causes an exceptionstring animal1 = "Cat";string animal2 = "Dog";int result;result = String.Compare(animal1, "Cat"); // result is zeroresult = String.Compare(animal2, "Cat"); // result is greater than zeroresult = String.Compare(animal1, animal2); // result is less than zerostring animal = "Cat";int result;result = String.Compare(animal, null); // result is greater than zeroresult = String.Compare(null, animal); // result is less than zeroresult = String.Compare(null, null); // result is zerostring animal1 = "Cat";string animal2 = "cat"; // Note use of lower caseint result;result = String.Compare(animal1, animal2, true); // Strings are equalresult = String.Compare(animal1, animal2, false); // Strings are not equal
-------------------------
متدی بنام AverageByColor بنام Pro اضافه شود به این صورت که یک رنگ به عنوان پارامتر
از ورودی دریافت کند و میانگین قیمت کالاهایی از آن رنگ را محاسبه و چاپ کند.
( به طور مثال ۱۰ تا کالا ۴ تا رنگ قرمز بگیرد جمع کالا و میانگین کالاها را محاسبه کند )
+ نوشته شده در شنبه بیست و پنجم آذر ۱۳۹۱ ساعت 19:0 توسط سید حسین اردهالی
|
حضرت علی (ع) می فرماید : علمی که در کار جلوه کند برترین نوع دانش است.