Mastering C# by Dennis Sharp

Mastering C# by Dennis Sharp

Author:Dennis Sharp
Language: eng
Format: epub
Tags: learn c# visual studio, learn c# from scratch, learn c# tutorials, learn c# in a day, learn c#, c sharp programming, unit testing c#, learn c in one day, c# step by step, how to learn c# for beginners
Publisher: Programmers's Corner
Published: 2019-12-21T16:00:00+00:00


Exercise

Design a Stopwatch Design a class called Stopwatch. The job of this class is to simulate a stopwatch. It should provide two methods: Start and Stop. We call the start method first, and the stop method next. Then we ask the stopwatch about the duration between start and stop. Duration should be a value in TimeSpan. Display the duration on the console. We should also be able to use a stopwatch multiple times. So we may start and stop it and then start and stop it again. Make sure the duration value each time is calculated properly. We should not be able to start a stopwatch twice in a row (because that may overwrite the initial start time). So the class should throw an InvalidOperationException if it’s started twice.

Learning tip: The aim of this exercise is to make you understand that a class should be always in a valid state. We use encapsulation and information hiding to achieve that. The class should not reveal its implementation detail. It only reveals a little bit, like a blackbox. From the outside, you should not be able to misuse a class because you should not be able to see the implementation detail.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.