Hi,
I have been programming C# for some time, and i recently learned about constructors and objects, but didn't fully understand.
Can anyone please help me understand this, and please spell it out. I have tried googling it, but couldn't really find anything, that made me understand it.

  • What is a constructor?
  • What is a object?
  • When should i use a constructor?
  • When should i use a object?

Dani AI

Generated

A short, practical primer that builds on the earlier replies from and the documentation pointer from . A class is the blueprint (what data and behavior a type exposes) and an object is a concrete instance created from that blueprint; objects carry identity, state and behavior and are the primary units of OOP modelling. (en.wikipedia.org)

A constructor is the special routine a type uses to get an instance into a valid initial state. C# has instance constructors (parameterless and parameterized) and a static (type) constructor for static initialization. If no instance constructors are declared, the compiler supplies a public parameterless constructor; declaring any constructor removes that auto-generated one. Static constructors run once before the type is used. (ecma-international.org)

Quick example (C#):

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }

    public Person() { }                      // simple parameterless constructor
    public Person(string name, int age)      // parameterized constructor
    {
        Name = name;
        Age = age;
    }

    public static Person CreateBaby(string name) => new Person(name, 0); // factory alternative
}

Constructors are appropriate when an instance cannot be valid without certain data or dependencies. Best practice: keep constructor work minimal (capture parameters, validate invariants), avoid long-running I/O or calling virtual members inside constructors, and prefer a factory method or Builder when creation is complex or needs multiple steps. For static initialization prefer inline initializers or a static constructor, but avoid throwing from static constructors because that can make a type unusable. (learn.microsoft.com)

Practical checklist: model a small real-world noun (Employee, Invoice), start with simple auto-properties and a single clear constructor that enforces required fields, run and inspect instances with new, then refactor to overloads, factory methods or a Builder only when construction becomes hard to use or error-prone.

Recommended Answers

All 4 Replies

You said you have been programming in C# for some time.
Did you manage to write a "Hello world" program? Great!
What else did you achieve in C#?
I'm not gonna reinvent the wheel here, I googled What is a constructor C# the first webpage is this one
If there is anything in there you don't understand, please come back and ask a question.
We will try to help.
See you soon.

commented: I'm seeing many folk dismiss such research. Maybe they are taught in school to not use research tools? +15

An object is a combination of some data and some methods that together represent some kind of entity eg an employee, an invoice, an IP address etc etc.
Constructors are special methods that are part of the object's definiton and are responsible for initialising the state of any new object when it is created.

You define an object when you want to represent something (see above) in your program. That definition includes the definition of one or more constructors if you need to initialise new objects to some particular value or state.

You say,

I have been programming C# for some time,

and

i recently learned about constructors and objects

Unless 'some time' is 'under an hour', these two statements are contradictory.

If you are new to C# programming, either learning it on your own or as a student in a course on the topic, we will understand, and be happy to help. However, if you try to present yourself as experienced in something when you aren't, that sets up expectations which will make it harder for us to help - we won't be able to judge what kind of explanation to give.

And if you try to pass off homework problems as something else, well, that's a Problem, as it is a direct violation of the Daniweb Forum Posting Rules, specifically this one:

  • Do provide evidence of having done some work yourself if posting questions from school or work assignments

We can forgive it once, as you are a new member who isn't clear on the rules, but please don't try it again.

commented: +1 for putting it nicely. +15

This looks more of a school asignment.
Also

I have been programming C# for some time, and i recently learned about constructors and objects, but didn't fully understand.

indeed looks like a contradictory statement.

The questions you've put here are the basic understanding of OOP, and are not tied specifically to C#. I'd advide you take a look over the concepts behind OOP.

I also found this Click Here link, of an online free course from Udemy, which tackles the Basics of OOP with C# (and yes, it covers your questions, 2nd module).

Hope this helps.

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.