Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Challenge: Robust Input Handling | Validating Input and Safe Parsing
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C# Exceptions and Error Handling Practice

bookChallenge: Robust Input Handling

Oppgave

Swipe to start coding

Write a method for a reservation system that validates and safely parses user input for a reservation date and the number of guests. The method should use TryParse for parsing and ensure the number of guests is a positive integer.

  • Parse the dateInput string to a DateTime value using DateTime.TryParse.
  • Parse the guestsInput string to an int value using int.TryParse.
  • Return true only if both parsing operations are successful and the number of guests is greater than zero.
  • Set the reservationDate and numberOfGuests out parameters to the parsed values if valid; otherwise, set them to their default values.

Løsning

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 6
single

single

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

Can you explain this in simpler terms?

What are the main benefits or drawbacks?

Can you give me a real-world example?

close

bookChallenge: Robust Input Handling

Sveip for å vise menyen

Oppgave

Swipe to start coding

Write a method for a reservation system that validates and safely parses user input for a reservation date and the number of guests. The method should use TryParse for parsing and ensure the number of guests is a positive integer.

  • Parse the dateInput string to a DateTime value using DateTime.TryParse.
  • Parse the guestsInput string to an int value using int.TryParse.
  • Return true only if both parsing operations are successful and the number of guests is greater than zero.
  • Set the reservationDate and numberOfGuests out parameters to the parsed values if valid; otherwise, set them to their default values.

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 6
single

single

some-alt