0

Confusing about constructor in Java and C# help!

hi guys. I need your help. I am confusing abou Java and C# For example, in Java we do: Class A{ private int name; public A(int name){ this.name = name; } } Class B extends A{ private String type; public B(String type, int name){ this.type = type; super(name); } } I have very difficulty to understand how it’s working in C#: class A{ private String nome; public A(String name){ this.name = name; } } class B : A{ private int idade; public B(int age, String name) : base(name){ this.age = age; } } I have an error in class B constructor

11th Mar 2019, 8:52 AM
Narushipudenn
Narushipudenn - avatar
3 Réponses
+ 4
In your c# code theres no error with B constructor you have done that right just need to fix the following and it should work 'nome' with 'name' 'idade' with 'age'
11th Mar 2019, 9:40 AM
D_Stark
D_Stark - avatar
0
right. was a mistake. I am trying this: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { //Console.Write.Line("its working"); } } class Animal{ private int age; private String name; Animal(int age, String name){ this.age = age; this.name = name; Console.WriteLine("Constructor!"); } ~Animal(){ Console.WriteLine("Destructor!"); } public int Age{ get{ return this.age; } set{ this.age = value; } } } class Dog : Animal{ Dog(int age, String name) : base(age, name){ } ~Dog(){ } } } and i get this error: ..\Playground\(44,31): error CS0122: 'Animal.Animal(int, string)' is inaccessible due to its protection level how to solve this?
11th Mar 2019, 10:31 AM
Narushipudenn
Narushipudenn - avatar
0
i have created get and set method
11th Mar 2019, 10:32 AM
Narushipudenn
Narushipudenn - avatar