Why do guys randomly mention that they have a girlfriend?

So there’s this guy from work I’ve always thought was attractive. Met him finally and mid convo he slipped in that he had a girlfriend. I didn’t ask but it somewhat fit into what we were speaking of but somewhat unnecessary. Why do guys do this? I was not fawning over him at all or showing my interest. Pretty casual convo with mostly work related stuff. Is it almost like a conscious reminder to himself or do guys say this when they’re cocky and think every girl is into them?
Why do guys randomly mention that they have a girlfriend?
Post Opinion