I'm doing this in visual C#.net.I'm a nwebie so bare with me.
I'm creating a class I need to declare three fields: name,phone number, and birthday (month,day, and year). I'm not sure how to declare them. Is this correct :?:

private string name
private string phone number
private int birthday

Dani AI

Generated

Good start, . and already pointed you toward the right concepts (correct identifiers and using properties for control). Below is a compact, practical pattern that keeps your object consistent and easy to validate or serialize later.

public class Person
{
    public string Name { get; private set; }
    public string PhoneNumber { get; private set; }
    public DateTime? Birthday { get; private set; }

    public Person(string name, string phoneNumber, DateTime? birthday = null)
    {
        if (string.IsNullOrWhiteSpace(name))
            throw new ArgumentException("Name is required", nameof(name));
        Name = name.Trim();

        PhoneNumber = NormalizePhone(phoneNumber);
        Birthday = birthday;
    }

    private static string NormalizePhone(string raw)
    {
        if (string.IsNullOrWhiteSpace(raw))
            return string.Empty;

        var sb = new System.Text.StringBuilder();
        foreach (var c in raw)
        {
            if (char.IsDigit(c) || (c == '+' && sb.Length == 0))
                sb.Append(c);
        }
        return sb.ToString();
    }
}

Notes and quick tips:

  • Use PascalCase for public properties (convention and tooling friendliness). The constructor enforces required values and keeps validation in one place as recommended.
  • Store phone numbers as strings and normalize them (example keeps digits and an optional leading '+'). For robust parsing/validation across countries, use a phone-number library rather than hand-rolled regex.
  • Birthday is nullable to represent unknown dates. If targeting .NET 6+, consider DateOnly instead of DateTime for a pure date value.
  • Keep fields private and prefer private setters or constructor-only initialization to maintain class invariants and make unit testing simpler.

This pattern keeps the class small, enforces valid state from creation, and makes future changes (formatting, validation rules, serialization) straightforward.

Recommended Answers

All 4 Replies

can't have spaces so phone number needs to be changed to phoneNumber
also, you probably want to use DateTime instead of int for the birthday. Good luck and keep at it

private string name;
private string phoneNumber;
private DateTime birthday;

Thanks for the help

anytime. Good questions normally get good answers around here.

maybe consider using a property for each of these. It would appear that you will want something outside of your class to change the values so you dont want to make your fields public (or internal) as you have no control over the values in there.
To create a property you need a private field and a property for each as follows:

private string _name;
public string name
{
  get 
     {
       return _name;
      }
  set
    {
      //do some validation if you want
      _name = value;
      //do some other stuff if you need to based on the new value
    }
}

You have full control when someone changes the value now.
I use them alot as private properties for just in time instantiation (the field is populated the first time the property is used so it isnt taking up memory.
Also you can make it read only by not using the set part of the property (leave it out altogether)
You can also have different accessors so the get part can be public and the set part can be private or internal.

Hope it helps you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.