Constructors in C#.NET Part 1 | C#.NET Tutorial | Mr. Bangar Raju — Transcript
Full transcript
- 0:02[Music]
- 0:07welcome to Nares technologist I'm bangar
- 0:09Raju and in this video uh I will
- 0:12demonstrate about
- 0:15Constructors so what is this Constructor
- 0:19see objectoriented programming is all
- 0:22about writing the code inside classes we
- 0:25just going to write the code inside a
- 0:27class and a class is a collect of
- 0:30various members like Fields methods
- 0:33Constructors etc etc and in that one of
- 0:36the most important member that is
- 0:38present in a class is going to be called
- 0:40as a Constructor so what is a
- 0:42Constructor means it is a special method
- 0:46present under a class responsible for
- 0:53initializing the variables of that class
- 1:00responsible for initializing the
- 1:02variables of that class so it's a
- 1:03special method present under a class
- 1:05responsible for initializing the
- 1:06variables of that
- 1:08class okay and now the main importance
- 1:12of a Constructor is it is responsible to
- 1:15initialize the variables inside your
- 1:17class what is that we'll talk on it just
- 1:19a minute meanwhile if it is a method it
- 1:21requires a name now and remember the
- 1:25name of a Constructor method the name of
- 1:28a Constructor method
- 1:31is exactly the same name of the class in
- 1:37which it was
- 1:40present the name of a Constructor method
- 1:43is exactly the same name of the class in
- 1:45which it was present so we can't change
- 1:47this thing now so if your class name is
- 1:49test your Constructor name is test if
- 1:50your class name is demo your Constructor
- 1:52name is demo you cannot change it and
- 1:55another important thing is and moreover
- 1:58it's a non
- 2:01value wring method it's a non-value
- 2:04wring method Constructor methods doesn't
- 2:06require written any value that doesn't
- 2:09written any value so just generally
- 2:11methods are two types value wring non
- 2:13value wring but Constructors are purely
- 2:15nonv value wring they never written any
- 2:18value and next each and every class
- 2:22requires this
- 2:25Constructor if we want to create
- 2:30the
- 2:31instance of that
- 2:34class this is another important Point
- 2:37each and every class requires this
- 2:38Constructor if we want to create the
- 2:40instance of that class if at all we want
- 2:43to create the instance of that
- 2:44particular class make sure your class
- 2:47contains a Constructor if I don't have a
- 2:49Constructor if you don't have a
- 2:50Constructor we cannot create the
- 2:52instance of the class but when I tell
- 2:55this point you'll be having a doubt what
- 2:57is the doubt earlier you might be
- 3:00writing classes and in the classes you
- 3:03may not Define anything called as
- 3:05Constructor but still you are able to
- 3:06create the instances of those classes
- 3:08something like this you defined a class
- 3:10like this class
- 3:11test in this class I'm declaring a
- 3:14variable called as in I something like
- 3:17this and now let us somewhere I want to
- 3:21create the instance of this class test
- 3:23obj is equals to new test create the
- 3:26instance of the class so is this
- 3:29statement Val valid yes it is valid we
- 3:31can create the instance but what the
- 3:33last point is telling here each and
- 3:34every class requires this Constructor if
- 3:36we want to create the instance of the
- 3:38class but here we are creating the
- 3:40instance but we don't have anything
- 3:41called as Constructor and if this is
- 3:45valid the last Point what we are talking
- 3:47here doesn't make any sense why without
- 3:51that Constructor we are telling instance
- 3:52cannot be created and here we don't have
- 3:55a Constructor but still we are able to
- 3:57create the instance so this will be a
- 3:59out what everyone will have in the mind
- 4:02so let me clarify that what is it it's
- 4:06the
- 4:08responsibility of a programmer to define
- 4:11a
- 4:13Constructor under his class it is the
- 4:16responsibility of a programmer to define
- 4:18a Constructor under his class
- 4:22and if he fails to do so on behalf of
- 4:28the programmer on behalf of the
- 4:31programmer an
- 4:33implicit
- 4:35Constructor gets defined in that class
- 4:41by the
- 4:44compiler on behalf of the programmer an
- 4:47implicit Constructor gets defined in
- 4:49that class by the compiler actually we
- 4:52need to do it but we did not do it if
- 4:54you don't do that on behalf of us the
- 4:58compiler will Define an implicit
- 5:00Constructor into that class so what
- 5:02happens you know this class after
- 5:06compilation this class after compilation
- 5:09will contain one implicit Constructor
- 5:11like this how
- 5:13public
- 5:15test and remember the name is test why
- 5:19the name is test because the Constructor
- 5:21name should exactly be the class name
- 5:23class name is test so the Constructor
- 5:25name is also test and inside this what
- 5:27will be present you know I is equals to
- 5:30Z what is that initializing the variable
- 5:34initializing the variable so why is the
- 5:37variable getting initialized here that
- 5:39is the responsibility of the Constructor
- 5:41that is the first point we're talking
- 5:43it's a special method present under a
- 5:45class responsible for initializing the
- 5:47variables of that class so if a class
- 5:50variable is initialized implicitly means
- 5:53that is done by a Constructor that is
- 5:55why every variable you declare directly
- 5:58under the class or every field you
- 6:00declare directly under a class has a
- 6:02default value if you don't give all
- 6:05numeric types are initialized with zeros
- 6:08and Boolean types are initialized with
- 6:10false string and object types are
- 6:11initialized with null means see in this
- 6:15I'm also going to declare a string
- 6:17string s did not initialize bull b did
- 6:20not initialize and now all the things
- 6:22will come here also string s and bulby
- 6:27but remember inside this Con structor
- 6:30all these are initialized s is equals to
- 6:32default value
- 6:34null and B is equals to default value
- 6:38false so like this the initialization is
- 6:42performed for each and every variable
- 6:45that is the role of the Constructor that
- 6:46is why Constructor is very important for
- 6:50us inside a class and if you don't have
- 6:53this Constructor with these variables
- 6:56will not be initialized because without
- 6:58we assigning a value a value coming
- 7:01there means there's not a magic show so
- 7:03someone has to assign the value here so
- 7:05who is going to do that the Constructor
- 7:08will do that and this Constructor is not
- 7:09defined by aember we did not Define The
- 7:12Constructor who defined this Constructor
- 7:14the compiler defined this Constructor
- 7:17for us and we call this as a implicit
- 7:20Constructor that is a Constructor that
- 7:22is implicitly defined and if we Define
- 7:25the same thing that is called as
- 7:26explicit Constructor if at all we are
- 7:29going to Define this Constructor
- 7:30explicitly we call that as a explicit
- 7:33Constructor okay otherwise every
- 7:35Constructor is going to be an implicit
- 7:37Constructor so you till now you might be
- 7:40defining 100 classes in all the 100
- 7:43classes you never defined a Constructor
- 7:46but still all those classes contain a
- 7:49Constructor and all those class
- 7:51Constructors are defined by the compiler
- 7:53for us and some few important points to
- 7:56remember here Point number one
- 8:03implicit
- 8:05Constructors implicitly defined
- 8:10Constructors
- 8:13Constructors are also known as default
- 8:19Constructors are also known as default
- 8:23Constructors why actually default
- 8:25Constructor means a Constructor which
- 8:28doesn't take any parameter see already
- 8:32on the top I said a point Constructor is
- 8:35a special method and why I'm just
- 8:38pointing this right now here is see
- 8:40method because of the method you can
- 8:43pass parameters to Method also there is
- 8:45a chance to pass
- 8:46parameters and if you pass any
- 8:48parameters to the particular thing the
- 8:50Constructor will become parameterized
- 8:52and right now there are no parameters so
- 8:54this is a parameterless Constructor this
- 8:57is going to be a parameterless
- 8:58Constructor so every parameterless
- 9:02Constructor will initialize the
- 9:04variables of the class with default
- 9:06values so that's
- 9:08way implicit Constructors are
- 9:11parameterless and parameterless
- 9:13Constructors are also known as default
- 9:16Constructor so let me change the
- 9:18definition implicitly defined
- 9:20Constructors
- 9:22are
- 9:24parameterless these Constructors are
- 9:27also known as default constu
- 9:29Constructors implicitly defined defined
- 9:34Constructors are parameterless and these
- 9:38Constructors are also known as default
- 9:40Constructors the first important point
- 9:42if a Constructor is implicitly defined
- 9:45don't forget that is parameterless and
- 9:48if it is parameterless they can Al they
- 9:51can also be called as default
- 9:53Constructors Point number two implicitly
- 9:56defined Constructors
- 10:00are
- 10:01public the constructors which are
- 10:03defined implicitly are public you notice
- 10:06said this class is a normal class here
- 10:09but when I defined the Constructor when
- 10:10I just said the Constructor gets defined
- 10:13I have mentioned here as a public why
- 10:16because implicitly defined Constructors
- 10:18are public they are going to be public
- 10:20Constructors here okay and another
- 10:25thing we can also Define a Constructor
- 10:29under the class we can also Define a
- 10:33Constructor under the class and if we
- 10:36Define it and if we Define it we can
- 10:41call it as explicit Constructor explicit
- 10:45Constructor and
- 10:49explicit
- 10:51Constructor and an explicit Constructor
- 10:54can be
- 10:57parameterless or
- 11:00parameterized also an explicit
- 11:03Constructor can be parameterless or
- 11:06parameterized but implicit Constructors
- 11:08cannot be parameterized implicit
- 11:11Constructors are
- 11:12parameterless but explicit Constructors
- 11:15can be parameterless or parameterized
- 11:17also two options are there for us both
- 11:19can be done okay fine let's just take a
- 11:23small example and define a Constructor
- 11:25okay here I've taken a project called as
- 11:27demo project in this I have class called
- 11:29program in this program class I wanted
- 11:32to define or I wanted to just declare a
- 11:35variable in I equals
- 11:38to uh or in
- 11:41simple okay
- 11:43next uh bub bu B there two
- 11:48variables now let me come here create
- 11:51the instance of the class program p is
- 11:54equals to new
- 11:56program and once after creating the
- 11:58instance of the
- 12:00class console do right
- 12:05line P do I the variable name is i p do
- 12:10I trying to print the I value as well as
- 12:14the B value also so one more time I'm
- 12:17writing P do
- 12:22B console do R
- 12:26line and do remember we did not assign
- 12:29any value for I and B but still when you
- 12:32run the code it prints
- 12:35values you can notice still you can see
- 12:39the values printed over there what are
- 12:41the values zero and F but don't forget
- 12:44we did not assign any value here we did
- 12:47not assign any value here and that value
- 12:51what has coming over there is a given by
- 12:54is given by The Constructor which
- 12:56Constructor implicit Constructor and we
- 12:59don't require to give that
- 13:03value uh let's just print this message a
- 13:06little bit clear what is it value of I
- 13:14is
- 13:18plus and value of B
- 13:22is plus so it's going to print both the
- 13:26two values for us
- 13:31okay so value of I is zero and the value
- 13:34of B is false it's just going to print
- 13:36the two values for us so that's what
- 13:38even if we did not assign any values you
- 13:41are able to see the value for I and B
- 13:44how the value came now the value came
- 13:47through the Constructor we did not
- 13:49assign so internally this class contains
- 13:53one implicit Constructor and that
- 13:55implicit Constructor is going to
- 13:58initialize the variables
- 14:00okay so can I define these Constructors
- 14:02explicitly you can Define how can I
- 14:04define a Constructor explicitly if you
- 14:06want to define a Constructor explicitly
- 14:09this is the syntax to define a
- 14:11Constructor
- 14:13modifiers
- 14:14name parameter
- 14:17list and write here so this is the
- 14:20syntax your modifiers name parameter
- 14:24list and then you're going to write your
- 14:26statements here so let us try defining a
- 14:29Constructor on our own now so right now
- 14:32we used an implicit Constructor here and
- 14:34that implicit Constructor is helping us
- 14:37in creating the instance and
- 14:38initializing the variables and you can
- 14:40Define this Constructors explicitly also
- 14:43how to define let's open the solution
- 14:48Explorer add a new class here
- 14:53add a
- 14:54class I'll name the class
- 14:57as X
- 14:59explicit con demo explicit Constructor
- 15:02demo simply call it as explicit con
- 15:06demo now in this class I want to write a
- 15:10Constructor no
- 15:11public explicit cond
- 15:14demo so this is an explicit Constructor
- 15:17what I defined inside this class public
- 15:20explicit condo this is an explicit
- 15:22Constructor okay and this Constructor if
- 15:26you don't Define also there'll be but
- 15:28implicit now it is explicit and here I'm
- 15:30writing a simple
- 15:31code console dot right
- 15:35line Constructor is called now
- 15:40static void
- 15:42main create the instance
- 15:46now explicit cond demo obj is equals to
- 15:50new explicit con demo creating the
- 15:54instance now we are creating the
- 15:57instance of the class now okay
- 15:59and afterwards console.
- 16:04rline and
- 16:05remember we are creating an instance and
- 16:09whenever you create an instance there
- 16:11will be a call to the Constructor there
- 16:14will be a call to the
- 16:19Constructor and I I can prove you this
- 16:22like this you can
- 16:25watch Constructor is called how many
- 16:28times times you create the instance that
- 16:31many number of times the Constructor
- 16:34will be called for
- 16:36you suppose I'm creating three instances
- 16:40obj1 obj2
- 16:42obj3 and you watch it every time you
- 16:45create the instance it will call the
- 16:48Constructor
- 16:50so if you notice the previous
- 16:53program in the previous program we did
- 16:56not Define any Constructor explicitly so
- 16:59the Constructor is implicit okay so
- 17:02right now I'm going to create the
- 17:04instance when I create the instance now
- 17:08even though the Constructor is implicit
- 17:09a call to the Constructor will take
- 17:11place so what is going to happen now it
- 17:13is going to call the implicit
- 17:14Constructor now and that implicit
- 17:17Constructor is now going to initialize
- 17:19the variables which variables I and B
- 17:22which zero and false so the point to
- 17:25understand is whenever the instance of a
- 17:28class is created
- 17:30then there is a implicit call to the
- 17:33appropriate Constructor why we are going
- 17:35to make the appropriate point this
- 17:37instance creation will call the
- 17:39Constructor okay means actually I should
- 17:42not use the word implicit here why I
- 17:44should call explicit only why because we
- 17:47are calling the Constructor explicitly
- 17:49where are we calling
- 17:52Watch program P what is program you can
- 17:55see there it is a class present under
- 17:58the demo project namespace it is a class
- 18:01present under the demo project
- 18:03namespace now come here when I say
- 18:05program and put my mouse here it is not
- 18:07showing you class what is it showing you
- 18:10program. program what is program.
- 18:12program the first program is a class and
- 18:15the second program is your Constructor
- 18:17means you are calling the Constructor
- 18:21explicitly you are calling the
- 18:23Constructor explicitly we are explicitly
- 18:26making a call to the Constructor and
- 18:29when we call the Constructor the
- 18:31implicit Constructor is called and will
- 18:34initialize the
- 18:36variables and coming here here also the
- 18:38same thing only you
- 18:41notice you notice this is your class
- 18:44name and this is your Constructor name
- 18:47and if you debbag you'll understand much
- 18:49better place a breako here by using F9
- 18:53so let us place a breakpoint here by
- 18:55using F9 and run the code
- 18:59watch when you added a breako over there
- 19:03when you added a break point the control
- 19:06will go and stop on that line when the
- 19:08execution is taking place just a second
- 19:11uh the control will come and stop on
- 19:13that particular line when you just over
- 19:15for executing the code now the control
- 19:18stopped on the line now press F1 now if
- 19:21you press F1 see what is going to happen
- 19:24so right now the control is on this line
- 19:26which line explicit con demo obj is
- 19:29equals to if you press
- 19:32F1 you see the control jumping to the
- 19:37Constructor the control jump to the
- 19:39Constructor now and after the control
- 19:41jump to the Constructor now it is
- 19:43executing the code inside the
- 19:45Constructor and coming back next again
- 19:50you're calling the sorry creating the
- 19:52second instance again goes to
- 19:54Constructor call again executes the
- 19:57logic comes back
- 19:59again third instance going to
- 20:01Constructor executing the logic and
- 20:04coming out of this so like this whenever
- 20:08the instance of any class is created the
- 20:12call is explicitly made to the
- 20:16Constructor here a point will be
- 20:19confusing for you a little bit what is
- 20:22it defining second one
- 20:26calling what is defining what is calling
- 20:29defining a Constructor
- 20:32means implementing one Constructor in
- 20:35your class so this is called defining
- 20:38this is called defining means we are
- 20:39explicitly writing the code for the
- 20:41appropriate thing that's called
- 20:43defining defining can be two types what
- 20:47implicit or explicit what is that
- 20:51implicit or explicit either you define
- 20:55it otherwise compiler will define it but
- 20:59calling what is calling whenever we the
- 21:02instance we are calling the Constructor
- 21:04calling is
- 21:07explicit you only should call implicit
- 21:10calling will not take place defining
- 21:12will be implicit but calling must be
- 21:14done explicitly if you don't call the
- 21:17Constructor explicitly that is not going
- 21:20to execute that may be an implicit
- 21:22Constructor or that may be an explicit
- 21:25Constructor but calling should be done
- 21:27explicitly by us if you explicitly call
- 21:30only the Constructor which is defined
- 21:32implicitly or which is defined
- 21:34explicitly will execute so this is the
- 21:38basic information for us about a
- 21:40Constructor okay uh in the next video I
- 21:43will just demonstrate about the various
- 21:45types of Constructors that are present
- 21:47in a class and how to work out with them
- 21:50thank you for watching the video for
- 21:51more videos please subscribe to our
- 21:53YouTube channel Nares it
- 21:57[Music]
- 21:58h
- 21:59[Music]
About this transcript
This page contains the full transcript of Constructors in C#.NET Part 1 | C#.NET Tutorial | Mr. Bangar Raju by Naresh i Technologies, generated from the public captions YouTube serves with the video. The transcript has 2,913 words across 479 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.