YouTube2Text

Constructors in C#.NET Part 1 | C#.NET Tutorial | Mr. Bangar Raju — Transcript

by Naresh i Technologies · 2,913 words · 479 segments · language en · Watch on YouTube

Full transcript

  1. 0:02[Music]
  2. 0:07welcome to Nares technologist I'm bangar
  3. 0:09Raju and in this video uh I will
  4. 0:12demonstrate about
  5. 0:15Constructors so what is this Constructor
  6. 0:19see objectoriented programming is all
  7. 0:22about writing the code inside classes we
  8. 0:25just going to write the code inside a
  9. 0:27class and a class is a collect of
  10. 0:30various members like Fields methods
  11. 0:33Constructors etc etc and in that one of
  12. 0:36the most important member that is
  13. 0:38present in a class is going to be called
  14. 0:40as a Constructor so what is a
  15. 0:42Constructor means it is a special method
  16. 0:46present under a class responsible for
  17. 0:53initializing the variables of that class
  18. 1:00responsible for initializing the
  19. 1:02variables of that class so it's a
  20. 1:03special method present under a class
  21. 1:05responsible for initializing the
  22. 1:06variables of that
  23. 1:08class okay and now the main importance
  24. 1:12of a Constructor is it is responsible to
  25. 1:15initialize the variables inside your
  26. 1:17class what is that we'll talk on it just
  27. 1:19a minute meanwhile if it is a method it
  28. 1:21requires a name now and remember the
  29. 1:25name of a Constructor method the name of
  30. 1:28a Constructor method
  31. 1:31is exactly the same name of the class in
  32. 1:37which it was
  33. 1:40present the name of a Constructor method
  34. 1:43is exactly the same name of the class in
  35. 1:45which it was present so we can't change
  36. 1:47this thing now so if your class name is
  37. 1:49test your Constructor name is test if
  38. 1:50your class name is demo your Constructor
  39. 1:52name is demo you cannot change it and
  40. 1:55another important thing is and moreover
  41. 1:58it's a non
  42. 2:01value wring method it's a non-value
  43. 2:04wring method Constructor methods doesn't
  44. 2:06require written any value that doesn't
  45. 2:09written any value so just generally
  46. 2:11methods are two types value wring non
  47. 2:13value wring but Constructors are purely
  48. 2:15nonv value wring they never written any
  49. 2:18value and next each and every class
  50. 2:22requires this
  51. 2:25Constructor if we want to create
  52. 2:30the
  53. 2:31instance of that
  54. 2:34class this is another important Point
  55. 2:37each and every class requires this
  56. 2:38Constructor if we want to create the
  57. 2:40instance of that class if at all we want
  58. 2:43to create the instance of that
  59. 2:44particular class make sure your class
  60. 2:47contains a Constructor if I don't have a
  61. 2:49Constructor if you don't have a
  62. 2:50Constructor we cannot create the
  63. 2:52instance of the class but when I tell
  64. 2:55this point you'll be having a doubt what
  65. 2:57is the doubt earlier you might be
  66. 3:00writing classes and in the classes you
  67. 3:03may not Define anything called as
  68. 3:05Constructor but still you are able to
  69. 3:06create the instances of those classes
  70. 3:08something like this you defined a class
  71. 3:10like this class
  72. 3:11test in this class I'm declaring a
  73. 3:14variable called as in I something like
  74. 3:17this and now let us somewhere I want to
  75. 3:21create the instance of this class test
  76. 3:23obj is equals to new test create the
  77. 3:26instance of the class so is this
  78. 3:29statement Val valid yes it is valid we
  79. 3:31can create the instance but what the
  80. 3:33last point is telling here each and
  81. 3:34every class requires this Constructor if
  82. 3:36we want to create the instance of the
  83. 3:38class but here we are creating the
  84. 3:40instance but we don't have anything
  85. 3:41called as Constructor and if this is
  86. 3:45valid the last Point what we are talking
  87. 3:47here doesn't make any sense why without
  88. 3:51that Constructor we are telling instance
  89. 3:52cannot be created and here we don't have
  90. 3:55a Constructor but still we are able to
  91. 3:57create the instance so this will be a
  92. 3:59out what everyone will have in the mind
  93. 4:02so let me clarify that what is it it's
  94. 4:06the
  95. 4:08responsibility of a programmer to define
  96. 4:11a
  97. 4:13Constructor under his class it is the
  98. 4:16responsibility of a programmer to define
  99. 4:18a Constructor under his class
  100. 4:22and if he fails to do so on behalf of
  101. 4:28the programmer on behalf of the
  102. 4:31programmer an
  103. 4:33implicit
  104. 4:35Constructor gets defined in that class
  105. 4:41by the
  106. 4:44compiler on behalf of the programmer an
  107. 4:47implicit Constructor gets defined in
  108. 4:49that class by the compiler actually we
  109. 4:52need to do it but we did not do it if
  110. 4:54you don't do that on behalf of us the
  111. 4:58compiler will Define an implicit
  112. 5:00Constructor into that class so what
  113. 5:02happens you know this class after
  114. 5:06compilation this class after compilation
  115. 5:09will contain one implicit Constructor
  116. 5:11like this how
  117. 5:13public
  118. 5:15test and remember the name is test why
  119. 5:19the name is test because the Constructor
  120. 5:21name should exactly be the class name
  121. 5:23class name is test so the Constructor
  122. 5:25name is also test and inside this what
  123. 5:27will be present you know I is equals to
  124. 5:30Z what is that initializing the variable
  125. 5:34initializing the variable so why is the
  126. 5:37variable getting initialized here that
  127. 5:39is the responsibility of the Constructor
  128. 5:41that is the first point we're talking
  129. 5:43it's a special method present under a
  130. 5:45class responsible for initializing the
  131. 5:47variables of that class so if a class
  132. 5:50variable is initialized implicitly means
  133. 5:53that is done by a Constructor that is
  134. 5:55why every variable you declare directly
  135. 5:58under the class or every field you
  136. 6:00declare directly under a class has a
  137. 6:02default value if you don't give all
  138. 6:05numeric types are initialized with zeros
  139. 6:08and Boolean types are initialized with
  140. 6:10false string and object types are
  141. 6:11initialized with null means see in this
  142. 6:15I'm also going to declare a string
  143. 6:17string s did not initialize bull b did
  144. 6:20not initialize and now all the things
  145. 6:22will come here also string s and bulby
  146. 6:27but remember inside this Con structor
  147. 6:30all these are initialized s is equals to
  148. 6:32default value
  149. 6:34null and B is equals to default value
  150. 6:38false so like this the initialization is
  151. 6:42performed for each and every variable
  152. 6:45that is the role of the Constructor that
  153. 6:46is why Constructor is very important for
  154. 6:50us inside a class and if you don't have
  155. 6:53this Constructor with these variables
  156. 6:56will not be initialized because without
  157. 6:58we assigning a value a value coming
  158. 7:01there means there's not a magic show so
  159. 7:03someone has to assign the value here so
  160. 7:05who is going to do that the Constructor
  161. 7:08will do that and this Constructor is not
  162. 7:09defined by aember we did not Define The
  163. 7:12Constructor who defined this Constructor
  164. 7:14the compiler defined this Constructor
  165. 7:17for us and we call this as a implicit
  166. 7:20Constructor that is a Constructor that
  167. 7:22is implicitly defined and if we Define
  168. 7:25the same thing that is called as
  169. 7:26explicit Constructor if at all we are
  170. 7:29going to Define this Constructor
  171. 7:30explicitly we call that as a explicit
  172. 7:33Constructor okay otherwise every
  173. 7:35Constructor is going to be an implicit
  174. 7:37Constructor so you till now you might be
  175. 7:40defining 100 classes in all the 100
  176. 7:43classes you never defined a Constructor
  177. 7:46but still all those classes contain a
  178. 7:49Constructor and all those class
  179. 7:51Constructors are defined by the compiler
  180. 7:53for us and some few important points to
  181. 7:56remember here Point number one
  182. 8:03implicit
  183. 8:05Constructors implicitly defined
  184. 8:10Constructors
  185. 8:13Constructors are also known as default
  186. 8:19Constructors are also known as default
  187. 8:23Constructors why actually default
  188. 8:25Constructor means a Constructor which
  189. 8:28doesn't take any parameter see already
  190. 8:32on the top I said a point Constructor is
  191. 8:35a special method and why I'm just
  192. 8:38pointing this right now here is see
  193. 8:40method because of the method you can
  194. 8:43pass parameters to Method also there is
  195. 8:45a chance to pass
  196. 8:46parameters and if you pass any
  197. 8:48parameters to the particular thing the
  198. 8:50Constructor will become parameterized
  199. 8:52and right now there are no parameters so
  200. 8:54this is a parameterless Constructor this
  201. 8:57is going to be a parameterless
  202. 8:58Constructor so every parameterless
  203. 9:02Constructor will initialize the
  204. 9:04variables of the class with default
  205. 9:06values so that's
  206. 9:08way implicit Constructors are
  207. 9:11parameterless and parameterless
  208. 9:13Constructors are also known as default
  209. 9:16Constructor so let me change the
  210. 9:18definition implicitly defined
  211. 9:20Constructors
  212. 9:22are
  213. 9:24parameterless these Constructors are
  214. 9:27also known as default constu
  215. 9:29Constructors implicitly defined defined
  216. 9:34Constructors are parameterless and these
  217. 9:38Constructors are also known as default
  218. 9:40Constructors the first important point
  219. 9:42if a Constructor is implicitly defined
  220. 9:45don't forget that is parameterless and
  221. 9:48if it is parameterless they can Al they
  222. 9:51can also be called as default
  223. 9:53Constructors Point number two implicitly
  224. 9:56defined Constructors
  225. 10:00are
  226. 10:01public the constructors which are
  227. 10:03defined implicitly are public you notice
  228. 10:06said this class is a normal class here
  229. 10:09but when I defined the Constructor when
  230. 10:10I just said the Constructor gets defined
  231. 10:13I have mentioned here as a public why
  232. 10:16because implicitly defined Constructors
  233. 10:18are public they are going to be public
  234. 10:20Constructors here okay and another
  235. 10:25thing we can also Define a Constructor
  236. 10:29under the class we can also Define a
  237. 10:33Constructor under the class and if we
  238. 10:36Define it and if we Define it we can
  239. 10:41call it as explicit Constructor explicit
  240. 10:45Constructor and
  241. 10:49explicit
  242. 10:51Constructor and an explicit Constructor
  243. 10:54can be
  244. 10:57parameterless or
  245. 11:00parameterized also an explicit
  246. 11:03Constructor can be parameterless or
  247. 11:06parameterized but implicit Constructors
  248. 11:08cannot be parameterized implicit
  249. 11:11Constructors are
  250. 11:12parameterless but explicit Constructors
  251. 11:15can be parameterless or parameterized
  252. 11:17also two options are there for us both
  253. 11:19can be done okay fine let's just take a
  254. 11:23small example and define a Constructor
  255. 11:25okay here I've taken a project called as
  256. 11:27demo project in this I have class called
  257. 11:29program in this program class I wanted
  258. 11:32to define or I wanted to just declare a
  259. 11:35variable in I equals
  260. 11:38to uh or in
  261. 11:41simple okay
  262. 11:43next uh bub bu B there two
  263. 11:48variables now let me come here create
  264. 11:51the instance of the class program p is
  265. 11:54equals to new
  266. 11:56program and once after creating the
  267. 11:58instance of the
  268. 12:00class console do right
  269. 12:05line P do I the variable name is i p do
  270. 12:10I trying to print the I value as well as
  271. 12:14the B value also so one more time I'm
  272. 12:17writing P do
  273. 12:22B console do R
  274. 12:26line and do remember we did not assign
  275. 12:29any value for I and B but still when you
  276. 12:32run the code it prints
  277. 12:35values you can notice still you can see
  278. 12:39the values printed over there what are
  279. 12:41the values zero and F but don't forget
  280. 12:44we did not assign any value here we did
  281. 12:47not assign any value here and that value
  282. 12:51what has coming over there is a given by
  283. 12:54is given by The Constructor which
  284. 12:56Constructor implicit Constructor and we
  285. 12:59don't require to give that
  286. 13:03value uh let's just print this message a
  287. 13:06little bit clear what is it value of I
  288. 13:14is
  289. 13:18plus and value of B
  290. 13:22is plus so it's going to print both the
  291. 13:26two values for us
  292. 13:31okay so value of I is zero and the value
  293. 13:34of B is false it's just going to print
  294. 13:36the two values for us so that's what
  295. 13:38even if we did not assign any values you
  296. 13:41are able to see the value for I and B
  297. 13:44how the value came now the value came
  298. 13:47through the Constructor we did not
  299. 13:49assign so internally this class contains
  300. 13:53one implicit Constructor and that
  301. 13:55implicit Constructor is going to
  302. 13:58initialize the variables
  303. 14:00okay so can I define these Constructors
  304. 14:02explicitly you can Define how can I
  305. 14:04define a Constructor explicitly if you
  306. 14:06want to define a Constructor explicitly
  307. 14:09this is the syntax to define a
  308. 14:11Constructor
  309. 14:13modifiers
  310. 14:14name parameter
  311. 14:17list and write here so this is the
  312. 14:20syntax your modifiers name parameter
  313. 14:24list and then you're going to write your
  314. 14:26statements here so let us try defining a
  315. 14:29Constructor on our own now so right now
  316. 14:32we used an implicit Constructor here and
  317. 14:34that implicit Constructor is helping us
  318. 14:37in creating the instance and
  319. 14:38initializing the variables and you can
  320. 14:40Define this Constructors explicitly also
  321. 14:43how to define let's open the solution
  322. 14:48Explorer add a new class here
  323. 14:53add a
  324. 14:54class I'll name the class
  325. 14:57as X
  326. 14:59explicit con demo explicit Constructor
  327. 15:02demo simply call it as explicit con
  328. 15:06demo now in this class I want to write a
  329. 15:10Constructor no
  330. 15:11public explicit cond
  331. 15:14demo so this is an explicit Constructor
  332. 15:17what I defined inside this class public
  333. 15:20explicit condo this is an explicit
  334. 15:22Constructor okay and this Constructor if
  335. 15:26you don't Define also there'll be but
  336. 15:28implicit now it is explicit and here I'm
  337. 15:30writing a simple
  338. 15:31code console dot right
  339. 15:35line Constructor is called now
  340. 15:40static void
  341. 15:42main create the instance
  342. 15:46now explicit cond demo obj is equals to
  343. 15:50new explicit con demo creating the
  344. 15:54instance now we are creating the
  345. 15:57instance of the class now okay
  346. 15:59and afterwards console.
  347. 16:04rline and
  348. 16:05remember we are creating an instance and
  349. 16:09whenever you create an instance there
  350. 16:11will be a call to the Constructor there
  351. 16:14will be a call to the
  352. 16:19Constructor and I I can prove you this
  353. 16:22like this you can
  354. 16:25watch Constructor is called how many
  355. 16:28times times you create the instance that
  356. 16:31many number of times the Constructor
  357. 16:34will be called for
  358. 16:36you suppose I'm creating three instances
  359. 16:40obj1 obj2
  360. 16:42obj3 and you watch it every time you
  361. 16:45create the instance it will call the
  362. 16:48Constructor
  363. 16:50so if you notice the previous
  364. 16:53program in the previous program we did
  365. 16:56not Define any Constructor explicitly so
  366. 16:59the Constructor is implicit okay so
  367. 17:02right now I'm going to create the
  368. 17:04instance when I create the instance now
  369. 17:08even though the Constructor is implicit
  370. 17:09a call to the Constructor will take
  371. 17:11place so what is going to happen now it
  372. 17:13is going to call the implicit
  373. 17:14Constructor now and that implicit
  374. 17:17Constructor is now going to initialize
  375. 17:19the variables which variables I and B
  376. 17:22which zero and false so the point to
  377. 17:25understand is whenever the instance of a
  378. 17:28class is created
  379. 17:30then there is a implicit call to the
  380. 17:33appropriate Constructor why we are going
  381. 17:35to make the appropriate point this
  382. 17:37instance creation will call the
  383. 17:39Constructor okay means actually I should
  384. 17:42not use the word implicit here why I
  385. 17:44should call explicit only why because we
  386. 17:47are calling the Constructor explicitly
  387. 17:49where are we calling
  388. 17:52Watch program P what is program you can
  389. 17:55see there it is a class present under
  390. 17:58the demo project namespace it is a class
  391. 18:01present under the demo project
  392. 18:03namespace now come here when I say
  393. 18:05program and put my mouse here it is not
  394. 18:07showing you class what is it showing you
  395. 18:10program. program what is program.
  396. 18:12program the first program is a class and
  397. 18:15the second program is your Constructor
  398. 18:17means you are calling the Constructor
  399. 18:21explicitly you are calling the
  400. 18:23Constructor explicitly we are explicitly
  401. 18:26making a call to the Constructor and
  402. 18:29when we call the Constructor the
  403. 18:31implicit Constructor is called and will
  404. 18:34initialize the
  405. 18:36variables and coming here here also the
  406. 18:38same thing only you
  407. 18:41notice you notice this is your class
  408. 18:44name and this is your Constructor name
  409. 18:47and if you debbag you'll understand much
  410. 18:49better place a breako here by using F9
  411. 18:53so let us place a breakpoint here by
  412. 18:55using F9 and run the code
  413. 18:59watch when you added a breako over there
  414. 19:03when you added a break point the control
  415. 19:06will go and stop on that line when the
  416. 19:08execution is taking place just a second
  417. 19:11uh the control will come and stop on
  418. 19:13that particular line when you just over
  419. 19:15for executing the code now the control
  420. 19:18stopped on the line now press F1 now if
  421. 19:21you press F1 see what is going to happen
  422. 19:24so right now the control is on this line
  423. 19:26which line explicit con demo obj is
  424. 19:29equals to if you press
  425. 19:32F1 you see the control jumping to the
  426. 19:37Constructor the control jump to the
  427. 19:39Constructor now and after the control
  428. 19:41jump to the Constructor now it is
  429. 19:43executing the code inside the
  430. 19:45Constructor and coming back next again
  431. 19:50you're calling the sorry creating the
  432. 19:52second instance again goes to
  433. 19:54Constructor call again executes the
  434. 19:57logic comes back
  435. 19:59again third instance going to
  436. 20:01Constructor executing the logic and
  437. 20:04coming out of this so like this whenever
  438. 20:08the instance of any class is created the
  439. 20:12call is explicitly made to the
  440. 20:16Constructor here a point will be
  441. 20:19confusing for you a little bit what is
  442. 20:22it defining second one
  443. 20:26calling what is defining what is calling
  444. 20:29defining a Constructor
  445. 20:32means implementing one Constructor in
  446. 20:35your class so this is called defining
  447. 20:38this is called defining means we are
  448. 20:39explicitly writing the code for the
  449. 20:41appropriate thing that's called
  450. 20:43defining defining can be two types what
  451. 20:47implicit or explicit what is that
  452. 20:51implicit or explicit either you define
  453. 20:55it otherwise compiler will define it but
  454. 20:59calling what is calling whenever we the
  455. 21:02instance we are calling the Constructor
  456. 21:04calling is
  457. 21:07explicit you only should call implicit
  458. 21:10calling will not take place defining
  459. 21:12will be implicit but calling must be
  460. 21:14done explicitly if you don't call the
  461. 21:17Constructor explicitly that is not going
  462. 21:20to execute that may be an implicit
  463. 21:22Constructor or that may be an explicit
  464. 21:25Constructor but calling should be done
  465. 21:27explicitly by us if you explicitly call
  466. 21:30only the Constructor which is defined
  467. 21:32implicitly or which is defined
  468. 21:34explicitly will execute so this is the
  469. 21:38basic information for us about a
  470. 21:40Constructor okay uh in the next video I
  471. 21:43will just demonstrate about the various
  472. 21:45types of Constructors that are present
  473. 21:47in a class and how to work out with them
  474. 21:50thank you for watching the video for
  475. 21:51more videos please subscribe to our
  476. 21:53YouTube channel Nares it
  477. 21:57[Music]
  478. 21:58h
  479. 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.