Soran
Bu kodlar ne yapar ? Büyükler el atabilir mi ? public string SeniorDeveloper( ) => "Dumur Oldum"

Genel Tartışma
-
Arkadaslar bold yazdıgım kodlar ne yapar, her bir kod parcasını alıp şunu yapar diye belirtirseniz memnun olurum. Bazı yerlerde ilk defa gördüğüm ifadeler var.
------------------------------------------------------------------------------------------------------------------------------------------------------------
static void Main()
{
var idTony = new EmployeeId("C3755");
var tony = new Employee(idTony, "Tony Stewart", 379025.00m);
var idCarl = new EmployeeId("F3547");
var carl = new Employee(idCarl, "Carl Edwards", 403466.00m);
var idKevin = new EmployeeId("C3386");
var kevin = new Employee(idKevin, "Kevin Harwick", 415261.00m);
var idMatt = new EmployeeId("F3323");
var matt = new Employee(idMatt, "Matt Kenseth", 1589390.00m);
var idBrad = new EmployeeId("D3234");
var brad = new Employee(idBrad, "Brad Keselowski", 322295.00m);
var employees = new Dictionary<EmployeeId, Employee>(31)
{
[idTony] = tony,
[idCarl] = carl,
[idKevin] = kevin,
[idMatt] = matt,
[idBrad] = brad
};
foreach (var employee in employees.Values)
{
WriteLine(employee);
}
while (true)
{
Write("Enter employee id (X to exit)> ");
var userInput = ReadLine();
userInput = userInput.ToUpper();
if (userInput == "X") break;
EmployeeId id;
try
{
id = new EmployeeId(userInput);
Employee employee;
if (!employees.TryGetValue(id, out employee))
{
WriteLine("Employee with id {0} does not exist", id);
}
else
{
WriteLine(employee);
}
}
catch (EmployeeIdException ex)
{
WriteLine(ex.Message);
}
}
}------------------------------------------------------------------------------------------------------------------------------------------------
public class Employee
{
private string _name;
private decimal _salary;
private readonly EmployeeId _id;
public Employee(EmployeeId id, string name, decimal salary)
{
_id = id;
_name = name;
_salary = salary;
}
public override string ToString() => $"{_id.ToString()}: {_name, -20} {_salary :C}";
}
---------------------------------------------------------------------------------------------------------------------------------
public class EmployeeIdException : Exception
{
public EmployeeIdException(string message) : base(message) { }
}
public struct EmployeeId : IEquatable<EmployeeId>
{
private readonly char _prefix;
private readonly int _number;
public EmployeeId(string id)
{
if (id == null) throw new ArgumentNullException(nameof(id));
_prefix = (id.ToUpper())[0];
int numLength = id.Length - 1;
try
{
_number = int.Parse(id.Substring(1, numLength > 6 ? 6 : numLength));
}
catch (FormatException)
{
throw new EmployeeIdException("Invalid EmployeeId format");
}
}
public override string ToString() => _prefix.ToString() + $"{_number,6:000000}";
public override int GetHashCode() => (_number ^ _number << 16) * 0x15051505;
public bool Equals(EmployeeId other) => (_prefix == other._prefix && _number == other._number);
public override bool Equals(object obj) => Equals((EmployeeId)obj);
public static bool operator ==(EmployeeId left, EmployeeId right) => left.Equals(right);
public static bool operator !=(EmployeeId left, EmployeeId right) => !(left == right);
}
----------------------------------------------------------------------------------------------------------------------------------
Bazı anlamadıgım kavramlar ;
if (id == null) throw new ArgumentNullException(nameof(id)); // Burası ne yapiyor ; nameof nedir ?
_prefix = (id.ToUpper())[0]; // Bu nedir Diziye mi cast ediyor diycem ortada dizi yok :( Dumur 1
_number = int.Parse(id.Substring(1, numLength > 6 ? 6 : numLength)); // Burayı biraz anladım gibi tam anlamadım :(
public override string ToString() => _prefix.ToString() + $"{_number,6:000000}"; // _number,6:000000} burası ne yapiyor formatlı yazdırma da tam olarak ne yapiyor
public override int GetHashCode() => (_number ^ _number << 16) * 0x15051505; // GetHashCode() ne yapar ? _number^_number bu üst mü aliyor ? ayrıca "<<" bu öperator nedir ?
public override bool Equals(object obj) => Equals((EmployeeId)obj); // Equals ne yapar ? nesneyi EmployeeId ye cast etmiş tekrar Equals almış :( ne yapmak istemiş
public static bool operator ==(EmployeeId left, EmployeeId right) => left.Equals(right); // Bundan hiç bişey anlamadım ilk defa görüyorum
public class EmployeeIdException : Exception
public static bool operator !=(EmployeeId left, EmployeeId right) => !(left == right); // Bundan hiç bişey anlamadım ilk defa görüyorum
{
public EmployeeIdException(string message) : base(message) { }
}Burası ne yapiyor :(
-----------------------------------------------------------------------------------------------------------------
KODLAR TEMİZ ŞEKİLDE :
static void Main() { var idTony = new EmployeeId("C3755"); var tony = new Employee(idTony, "Tony Stewart", 379025.00m); var idCarl = new EmployeeId("F3547"); var carl = new Employee(idCarl, "Carl Edwards", 403466.00m); var idKevin = new EmployeeId("C3386"); var kevin = new Employee(idKevin, "Kevin Harwick", 415261.00m); var idMatt = new EmployeeId("F3323"); var matt = new Employee(idMatt, "Matt Kenseth", 1589390.00m); var idBrad = new EmployeeId("D3234"); var brad = new Employee(idBrad, "Brad Keselowski", 322295.00m); var employees = new Dictionary<EmployeeId, Employee>(31) { [idTony] = tony, [idCarl] = carl, [idKevin] = kevin, [idMatt] = matt, [idBrad] = brad }; foreach (var employee in employees.Values) { WriteLine(employee); } while (true) { Write("Enter employee id (X to exit)> "); var userInput = ReadLine(); userInput = userInput.ToUpper(); if (userInput == "X") break; EmployeeId id; try { id = new EmployeeId(userInput); Employee employee; if (!employees.TryGetValue(id, out employee)) { WriteLine("Employee with id {0} does not exist", id); } else { WriteLine(employee); } } catch (EmployeeIdException ex) { WriteLine(ex.Message); } } }
public class Employee { private string _name; private decimal _salary; private readonly EmployeeId _id; public Employee(EmployeeId id, string name, decimal salary) { _id = id; _name = name; _salary = salary; } public override string ToString() => $"{_id.ToString()}: {_name, -20} {_salary :C}"; }
public class EmployeeIdException : Exception { public EmployeeIdException(string message) : base(message) { } } public struct EmployeeId : IEquatable<EmployeeId> { private readonly char _prefix; private readonly int _number; public EmployeeId(string id) { if (id == null) throw new ArgumentNullException(nameof(id)); _prefix = (id.ToUpper())[0]; int numLength = id.Length - 1; try { _number = int.Parse(id.Substring(1, numLength > 6 ? 6 : numLength)); } catch (FormatException) { throw new EmployeeIdException("Invalid EmployeeId format"); } } public override string ToString() => _prefix.ToString() + $"{_number,6:000000}"; public override int GetHashCode() => (_number ^ _number << 16) * 0x15051505; public bool Equals(EmployeeId other) => (_prefix == other._prefix && _number == other._number); public override bool Equals(object obj) => Equals((EmployeeId)obj); public static bool operator ==(EmployeeId left, EmployeeId right) => left.Equals(right); public static bool operator !=(EmployeeId left, EmployeeId right) => !(left == right); }
IEquatable arayüzü ne yapar ?
Cok soru sordum biliyorum ;
struct EmployeeId yapısının içini ve main fonksiyonun icini belirtidiğim bold olan yerlerde sıkıntım var
Bunu yazmayı unutmuşum :) employees.TryGetValue(id, out employee) ne yapar :D- Düzenleyen yuKKo Ganioglu 5 Ekim 2016 Çarşamba 21:17
- Değiştirilmiş Tür Kyamuran SalibryamMicrosoft contingent staff, Moderator 17 Ekim 2016 Pazartesi 08:10
Tüm Yanıtlar
-
-
-
nameof instance'ın class,method vs. adını string olarak verir. Benim en çok MVC de işime yarıyor;
<a href="@Url.Action(nameof(HomeController.Index))">anne sayfa</a>
e-mail: onay[nokta]yalciner[at]hotmail[nokta]com
- Düzenleyen Önay YALÇINERModerator 6 Ekim 2016 Perşembe 07:15
-
employees.TryGetValue(id, out employee)
Dictionary nesnesinden verilen keye göre out ile işaretlenmiş referansa instance geçirmeye çalışır ve başarılı olup olmadığına göre boolean sonuç döndürür. Bu şu demek key varsa employee nesnesine ata ve bana true döndür olmadı yoksa false döndür.
Fullstack Developer
-
nameof instance'ın class,method vs. adını string olarak verir. Benim en çok MVC de işime yarıyor;
<a href="@Url.Action(nameof(HomeController.Index))">anne sayfa</a>
e-mail: onay[nokta]yalciner[at]hotmail[nokta]com
Önay bey ; Hep Onay diyorum :)
public override int GetHashCode() => (_number ^ _number << 16) * 0x15051505;
GetHashCode() ne yapar ? _number ^ _number bu ifade nedir ? aynı zamanda _number << 16 bu ifade nedir
_number >>16 böyle olursa ne oluyor ?
public override string ToString() => _prefix.ToString() + $"{_number,6:000000}"; // Bu formatlı yazdırma nasıl formatlıyor ?
Ve;
public static bool operator ==(EmployeeId left, EmployeeId right) => left.Equals(right);
public static bool operator !=(EmployeeId left, EmployeeId right) => !(left == right);Bu ifadeler ne yapiyor böyle bir kullanımı ilk defa görüyorum :(
-----------------
Bu ifadeler ;
public bool Equals(EmployeeId other) => (_prefix == other._prefix && _number == other._number);
public override bool Equals(object obj) => Equals((EmployeeId)obj);IEquatable<EmployeeId> arayüzünün implmente edilmesiyle gelmiş sanırım bold yazdıgım , sonra alt satirda neden override etmiş amacı ne bu gavurun ?
BigBoss Amonra :)
-
Operatörleri incelemen lazım: https://msdn.microsoft.com/en-us/library/6a71f45d.aspx
^ bu XOR operatörü, exclusive or, iki bitin farklı olduğu durumlarda true, aynı ise false döndürür.
10011101
11001100
-------------xor
01010001 çıkar<< left shift, bitleri sağdaki operant kadar sola kaydırır, (bi anlamda kuvvetini alır)
00010011
------------- << 1 (burda 2 ile çarpmuş gibi olur)
0010011000010011
------------- << 2 burada (2 defa 2 ile çarpmış gibi olur)
01001100GetHashCode ise duruma göre değişen bişey, kendisi de virtual zaten. GetHashCode instance ların aynı olup olmadığına bakmak için kullanlması için tasarlanmış bişey. Senin örneğinde GetHashCode'u override edip instance'ın hash codunu üretme kuralı bit kaydırma, xor ve çarpma işlemiyle belirlenmiş.
e-mail: onay[nokta]yalciner[at]hotmail[nokta]com
- Düzenleyen Önay YALÇINERModerator 8 Ekim 2016 Cumartesi 19:01
-
Operatörleri incelemen lazım: https://msdn.microsoft.com/en-us/library/6a71f45d.aspx
^ bu XOR operatörü, exclusive or, iki bitin farklı olduğu durumlarda true, aynı ise false döndürür.
10011101
11001100
-------------xor
01010001 çıkar<< left shift, bitleri sağdaki operant kadar sola kaydırır, (bi anlamda kuvvetini alır)
00010011
------------- << 1 (burda 2 ile çarpmuş gibi olur)
0010011000010011
------------- << 2 burada (2 defa 2 ile çarpmış gibi olur)
01001100GetHashCode ise duruma göre değişen bişey, kendisi de virtual zaten. GetHashCode instance ların aynı olup olmadığına bakmak için kullanlması için tasarlanmış bişey. Senin örneğinde GetHashCode'u override edip instance'ın hash codunu üretme kuralı bit kaydırma, xor ve çarpma işlemiyle belirlenmiş.
e-mail: onay[nokta]yalciner[at]hotmail[nokta]com
Onay bey;
public static bool operator ==(EmployeeId left, EmployeeId right) => left.Equals(right);
public static bool operator !=(EmployeeId left, EmployeeId right) => !(left == right);Arkadaş operator overload demiş ; Method overload methodun farklı parametreli versiyonu. Operator overload anlamadım :( internetde örnek bulamadım anlaşılır; ilk defa bu ifadeleri görüyorum .Fonksiyon desem benzemiyor :/
Amaç nedir basit bir örnekle acıklarsanız memnun olurum OnaY Bey ,
-
class Dörtgen { public int En { get; set; } public int Boy { get; set; }
public static bool operator == (Dörtgen d1, Dörtgen d2)
{
return
((d1.En == d2.En) && (d1.Boy == d2.Boy))
||
((d1.En == d2.Boy) && (d1.Boy == d2.En))
} }Normalde Dörtgen classımın iki instance'ı arasında == operatörü istediğim sonucu vermez. Equals kullansam bile bu gethashcode kullanacağından true döndürmeyecektir. Ama == operatörünü overload ederek iki Dörtgenin hangi koşullarda eşit olduğunu tanımladım. Bununla birlikte
if(dörtgen1 == dörtgen2) { ... }
gibi bişey kullanabilirim.
Buna GetHashCode un overloadını da eklersem ve En ve Boy a göre aynı kodun üretilmesini sağlarsam
public override int GetHashCode() => this.En ^ this.Boy;
Equals da true döndürmeye başlar.
Ek: bir class'da == operatörü overload ederseniz != operatörünüde overload etmeniz gerekir, aynı kural < ve > için de geçerli
e-mail: onay[nokta]yalciner[at]hotmail[nokta]com
- Düzenleyen Önay YALÇINERModerator 9 Ekim 2016 Pazar 17:59