Object Initializers and Collection Initializers

Object Initializers and Collection Initializers allow us to specify a set of properties to set on a new object or items to add to a new collection within a single expression

Old style:

var customer = new Customer();
customer.Name = "Phani";
customer.Address = "UK";
var item1 = new OrderItem();
item1.ItemId = "a123";
item1.Quantity = 5;
var item2 = new OrderItem();
item2.ItemId = "b456";
item2.Quantity = 2;
var order = new Order();
order.OrderId = "xyz";
order.Customer = customer;
order.Items.Add(item1);
order.Items.Add(item2);

Now with object and collection initializers , we can write as :

var order = new Order
{
    OrderId = "xyz",
    Customer = new Customer { Name = "Phani", Address = "UK" },
    Items =
    {
        new OrderItem { ItemId = "a23", Quantity = 5 },
        new OrderItem { ItemId = "b456", Quantity = 2 }
    }
};

Github: https://github.com/pbndru/ObjectCollectionInitializers

Tuples

Although, technically, a method can have only one return type, the return type could be a tuple. As a result, starting with C# 7.0, it is possible to return multiple values packaged as a tuple using C# tuple syntax. For example, you could declare a GetName() method as

(string First, string Last) name = GetName();

static (string First, string Last) GetName()
{
string firstName, lastName;
firstName = GetUserInput(“Please enter first name”);
lastName = GetUserInput(“Please enter last name”);

return (firstName, lastName);
}

Please find the code in my github: https://github.com/pbndru/Tuples


Implicit and Explicit casting

Explicit Cast

In C#, you cast using the cast operator. By specifying the type you would like the variable converted to within parentheses, you acknowledge that if an explicit cast is occurring, there may be a loss of precision and data, or an exception may result

Explicit Casting:

long longNumber = 509876543456;

int intNumber = (int) longNumber;

In other instances, such as when going from an int type to a long type, there is no loss of precision, and no fundamental change in the value of the type occurs. In these cases, the code needs to specify only the assignment operator; the conversion is implicit. In other words, the compiler is able to determine that such a conversion will work correctly

//  Not Using the Cast Operator for an Implicit Conversion 
int intNumber = 31416;
long longNumber = intNumber;

Strings Are Immutable

A key characteristic of the string type is that it is immutable. A string variable can be assigned an entirely new value, but there is no facility for modifying the contents of a string. It is not possible, therefore, to convert a string to all uppercase letters. It is trivial to create a new string that is composed of an uppercase version of the old string, but the old string is not modified in the process

//String is immutable
class Uppercase
{
  static void Main()
  {
      string text;

      System.Console.Write("Enter text: ");
      text = System.Console.ReadLine();

      // UNEXPECTED:  Does not convert text to uppercase                    
      text.ToUpper();                                                       

      System.Console.WriteLine(text);
  }
}

At a glance, it would appear that text.ToUpper() should convert the characters within text to uppercase. However, strings are immutable and, therefore, text.ToUpper() will make no such modification. Instead, text.ToUpper() returns a new string that needs to be saved into a variable or passed to System.Console.WriteLine() directly

class Uppercase
{
  static void Main()
  {
      string text, uppercase;

      System.Console.Write("Enter text: ");
      text = System.Console.ReadLine();

      // Return a new string in uppercase
      uppercase = text.ToUpper();                              

      System.Console.WriteLine(uppercase);
  }
}

Please find the code in my Github: https://github.com/pbndru/StringImmutable

String Interpolation

This feature inserts values into a string with simple syntax. It is similar to string.Format, but variables may be accessed directly (not through index arguments)

C# program that uses string interpolation

using static System.Console;

class Program
{
    static void Main()
    {
        int cats = 100;
        int dogs = 2;

        // Create a string with string interpolation syntax.
        string animals = $"cats = {cats} and dogs = {dogs}";
        WriteLine(animals);
    }
}

Output

cats = 100 and dogs = 2