Working on C# Category in QuizUp

For those of you who are QuizUp players (quizup.com), use the link below to access a custom quiz that I’ve created for C#.

http://quizup.com/topics/_e44d9e38-f60d-4ab3-8f5b-6a04905fc386

There are very few questions in the quiz at the moment, more coming soon.

I’m looking, however, for volunteers who can verify that they are able to use this link to access the C# category on their mobile device.

Let me know if you’re able to get to the quiz. Thanks!

 

#1,220 – C# 6.0 – Defining a Parameterless Constructor for a struct

In C# 5.0, every struct had a default parameterless constructor that you couldn’t override.  Using the new operator invoked this constructor and all members of the struct were assigned default values.

    public struct MyPoint
    {
        public double X;
        public double Y;
    }

    class Program
    {
        static void Main(string[] args)
        {
            MyPoint pt = new MyPoint();      // default values for X, Y (0.0)
            Console.WriteLine("{0}, {1}", pt.X, pt.Y);
        }
    }

1220-002

In C# 6.0, you can explicitly define a parameterless constructor for a struct, giving non-default values to the members of the struct.

        public struct MyPoint
        {
            public double X;
            public double Y;

            public MyPoint()
            {
                X = 100.0;
                Y = 100.0;
            }
        }

        static void Main(string[] args)
        {
            MyPoint pt = new MyPoint();      // 100.0, 100.0
            Console.WriteLine("{0}, {1}", pt.X, pt.Y);
        }

1220-001

#1,219 – C# 6.0 – Filtering Exceptions

C# 6.0 will include support for exception filters, that is–only catching a particular type of exception if an associated expression evaluates to true.

You can filter exceptions by including an if statement after the catch expression.  If the result of evaluating the expression supplied is true, the exception is caught.  If not, the behavior is as if you didn’t supply a catch block.

In the example below, we don’t catch divide by zero exceptions on Saturdays.

            int denom;
            try
            {
                denom = 0;
                int x = 5 / denom;
            }
            // Catch /0 on all days but Saturday
            catch (DivideByZeroException xx) if (DateTime.Now.DayOfWeek != DayOfWeek.Saturday)
            {
                Console.WriteLine(xx);
            }

#1,218 – C# 6.0 – Using Lambdas for Getter-Only Auto-Properties

In addition to using lambda expressions for method bodies in C# 6.0, you can use a lambda expression to implement the getter of a read-only auto-property.

For example:

        public string Name { get; protected set; }
        public int Age { get; set; }

        public string BackwardsName => new string(Name.Reverse().ToArray());

The presence of the lambda expression tells the compiler that this is a property with a getter, rather than a field.

#1,217 – C# 6.0 – Using Lambda Expressions for Function Members

In C# 5.0, you could use a lambda expression wherever a delegate instance was expected.  For example:

            Func<int,int> doubleMyNumber = (i) => 2 * i;

In C# 6.0, you can also use a lambda expression as the body of a function member. This simplifies the syntax for simple methods.  For example:

    public class Dog
    {
        public Dog(string name, int age)
        {
            Name = name;
            Age = age;
        }

        public string Name { get; protected set; }
        public int Age { get; set; }

        public void AgeIncrement() => Age++;
        public int AgeInDogYears() => Age * 7;
    }

#1,216 – C# 6.0 – Initializing Read-Only Auto-Properties from Constructors

NOTE: The following is proposed syntax for Roslyn, but not yet working in the Visual Studio 2014 CTP, as of 30 Oct 2014.  The Codeplex site lists this as a “Done” feature for Roslyn, but it’s not absolutely clear whether this will ship with Visual Studio 2014.

In C# 6.0, you’ll be able to define read-only auto-properties you can initialize the property as part of its declaration.

  public DateTime DogCreationTime { get; } = DateTime.Now;

The intent in C# 6.0 is that you’ll also be able to initialize these read-only auto-properties from a constructor.  For example:

    public class Dog
    {
        public string Name { get; }

        public Dog(string name)
        {
            Name = name;
        }
    }

#1,215 – C# 6.0 – New Syntax for Dictionary Initializers

Prior to C# 6.0, you could initialize a Dictionary object with a collection initializer as follows:

            // Initialization with collection initializer
            Dictionary<string, int> guys = new Dictionary<string, int> {
                {"Galileo", 1564},
                {"Magellan", 1480},
                {"Voltaire", 1694},
                {"Kepler", 1571},
                {"Keaton", 1895}
            };

In C# 6.0, the following syntax will also work:

            // Initialization with new dictionary initializer
            Dictionary<string, int> guys = new Dictionary<string, int>
            {
                ["Galileo"] = 1564,
                ["Magellan"] = 1480,
                ["Voltaire"] = 1694,
                ["Kepler"] = 1571,
                ["Keaton"] = 1895
            };

Note that as of 29 Oct, 2014, this feature is not enabled in the current Visual Studio 2014 CTP.

Follow

Get every new post delivered to your Inbox.

Join 576 other followers