Practicing my autodidactism using a free Kindle Unlimited book to get myself acquainted with the basics of C# in hopes of getting enough of a foundation to get into automation and I still feel a little wibbly on the best practices, etc and I just feel like I could've been a bit more elegant than what I enacted with the get; set; but I don't know what I don't know.
"C#: Learn C# in One Day and Learn it Well" in this instance. Which, I've exceeded a day by far... anyways, I've been challenging myself to use what I have learnt in prior chapters to unravel and actively enact theoretical applications in the book without any actual 'code-alongside' snippets when they rear their head.
I'm current learning about var and their usage in using LINQ queries which is fairly easy as I've prior experience in SQL but I feel like I've inelegantly accessed/set the values for what cust.Balance and cust.Name are pulling in the code with the get; set; in the snippet below and feel like there's some way to have accessed them without actively creating another set of public strings.
Is there a better way to have executed this?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LinqTut2
{
class Program
{
static void Main(string[] args)
{
List<Customer> customers = new List<Customer>();
customers.Add(new Customer("Alan", "80911291", "ABC Street", 25.60m));
customers.Add(new Customer("Bill", "19872131", "DEF Street", -32.10m));
customers.Add(new Customer("Carl", "29812371", "GHI Street", -12.2m));
customers.Add(new Customer("David", "78612312", "JKL Street", 12.6m));
var overdue =
from cust in customers
where cust.Balance < 0
orderby cust.Balance ascending
select new { cust.Name, cust.Balance };
foreach (var cust in overdue)
Console.WriteLine("Name = {0}, Balance = {1}", cust.Name, cust.Balance);
Console.ReadLine();
}
}
class Customer
{
private string CustName;
private string CustPhone;
private string CustAddress;
private decimal CustBalance;
public decimal Balance
{
get
{
return CustBalance;
}
set
{
CustBalance = value;
}
}
public string Name
{
get
{
return CustName;
}
set
{
CustName = value;
}
}
public Customer(string name, string phn, string addr, decimal bal)
{
CustName = name;
CustPhone = phn;
CustAddress = addr;
CustBalance = bal;
}
}
}