C# overloaded constructors 🍕

preview_player
Показать описание
C# overloaded constructors tutorial example explained

#C# #overloaded #constructors

using System;

namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
// overloaded constructors = technique to create multiple constructors,
// with a different set of parameters.
// name + parameters = signature

Pizza pizza = new Pizza("stuffed crust", "red sauce", "mozzarella");

Console.ReadKey();
}
}
class Pizza
{
String bread;
String sauce;
String cheese;
String topping;

public Pizza(String bread)
{
}
public Pizza(String bread, String sauce)
{
}
public Pizza(String bread, String sauce, String cheese)
{
}
public Pizza(String bread, String sauce, String cheese, String topping)
{
}
}
}
Рекомендации по теме
Комментарии
Автор

using System;

namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
// overloaded constructors = technique to create multiple constructors,
// with a different set of parameters.
// name + parameters = signature

Pizza pizza = new Pizza("stuffed crust", "red sauce", "mozzarella");

Console.ReadKey();
}
}
class Pizza
{
String bread;
String sauce;
String cheese;
String topping;

public Pizza(String bread)
{
this.bread = bread;
}
public Pizza(String bread, String sauce)
{
this.bread = bread;
this.sauce = sauce;
}
public Pizza(String bread, String sauce, String cheese)
{
this.bread = bread;
this.sauce = sauce;
this.cheese = cheese;
}
public Pizza(String bread, String sauce, String cheese, String topping)
{
this.bread = bread;
this.sauce = sauce;
this.cheese = cheese;
this.topping = topping;
}
}
}

BroCodez
Автор

Lets just appreciate him for making the video 4:20 minutes long

bnceee
Автор

Omg thank you so much!! You literally saved my life. First day of my sem2, my professor went on a rant of overloaded constructors and used confusing words for a first year and expected we knew before coming to her class. Literally went on an almost one-hour lesson about it with no conclusion that a classmate fell asleep and began snoring! I switched professors after I got home that night. Still, it irked me so I decided to go on YouTube.. She could have literally just showed us this video if she can't explain.

LenssiaYT
Автор

Thank you so much for this video. I've been really struggling understanding the topic and you've helped tremendously.

larralu
Автор

Going off your example

How or what would be the point of having 4 constructors all baked pizza

What would that end up doing or what would you benefit from having it set up like how you do?

marineoorahu
Автор

if (topping == pinapple)
enviorment.Exit(1)

ZertumErtaka
Автор

We can use params in constructors too so it can receive a variable number of arguments .

Main class : Test1

using Test1;

String[] a = { "Alex", "13"};
String[] b = { "Alex", "13", "North pole" };

Human man1 = new Human("Alex");
Human man2 = new Human("Alex", "13");
Human man3 = new Human("Alex", "13", "North pole");

man1.Intro();
Console.WriteLine("");
man2.Intro();
Console.WriteLine("");
man3.Intro();

->
I am Alex .

I am Alex .
I am 13 years old .

I am Alex .
I am 13 years old .
I am from North pole .

Self-defined class : Human

public String name, age, country;

public Human(params String[] a)
{
int x = 0;

foreach (String i in a)
{
x += 1;
switch (x)
{
case 1:
this.name = a[0];
break;
case 2:
this.age = a[1];
break;
case 3:
this.country = a[2];
break;
default:
break;
}
}
}

public void Intro()
{
if (!(name == null)) {
Console.WriteLine($"I am {name} .");
}

if (!(age == null))
{
Console.WriteLine($"I am {age} years old .");
}

if (!(country == null))
{
Console.WriteLine($"I am from {country} .");
}
}

maxwong
Автор

Otherwise this is a good example, but I think you should have pointed out that you can not have two constructors with the same amount and the same types of parameters. That is, you can not make two pizzas such that the other one has bread, sauce, and cheese (but no topping), and the other one has bread, sauce, and topping (but no cheese).

ajlakanen
Автор

It's all so clear now... Thank you so much!!!

bootcampnikki
Автор

This was a lot easier then a teacher just telling you to use overloading. . .

QuestionTheTruth
Автор

so just like overloaded methods where we use keyword params can we use params as well on constructors? Sorry if i type something wrong but it should look something like this: public void Pizza(params String[] ingredients){ if(String.Length>0) this.bread = ingedients[0] if(String.Length >1 this.sauce = ingredients[1] etc.) How practical is this approach?

qwezrxt
Автор

How about using default values in the constructor?

lunaticberserker
Автор

What's the difference between this and using default parameters?

Fusionultra
Автор

I dont know if something changed but its saying I have to make the fields nullable so in order for my code to work I have to say:

Class Pizza
{
String bread;
String? sauce;
String; cheese;
String? topping;
and so on

Is this a new thing since this video was posted or did i do something wrong?

digineet
Автор

But the constructor cannot have different names right ?

venkatramana
Автор

I am just wandering why not to use arrays in constructors as arguments

vagifgafar
Автор

So we cant overwrite one of those fields by asking for an input?

Konstatine
Автор

"pineapple belongs on pizza"
Thank you mate , you got a like and a sub and a share

zeux
Автор

Can i use params keyword in constructor?

waltergermanes
Автор

Is it okay if you just write bread = bread instead of this.bread = bread? is the this keyword really needed?

kevind