Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,026 questions

51,982 answers

573 users

How to use constructor in C#

4 Answers

0 votes
using System;
 
namespace ConsoleApplication
{
    public class Test
    {
        public bool constructor_active = false;
 
        public Test() { // constructor
            constructor_active = true;
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            try {
                Test t = new Test();
                Console.WriteLine(t.constructor_active);
            }
            catch (Exception ex) {
                Console.WriteLine(ex.Message);
            }
        }
    }
}
 
 
 
/*
run:
      
True
    
*/

 



answered Sep 7, 2015 by avibootz
edited May 4, 2024 by avibootz
0 votes
using System;
 
namespace ConsoleApplication
{
    public class Worker
    {
        private float salary;
 
        public Worker(int global_salary) { // constructor 1
            salary = global_salary;
        }
 
        public Worker(float day_salary , int days) { // constructor 2
            salary = day_salary * days;
        }
 
        public void show() {
            Console.WriteLine("Salary : {0}", salary);
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            try {
                Worker w1 = new Worker(13084);
                w1.show();
 
                Worker w2 = new Worker(457, 25);
                w2.show();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}


 
/*
run:
      
Salary : 13084
Salary : 11425
    
*/

 



answered Sep 7, 2015 by avibootz
edited May 4, 2024 by avibootz
0 votes
using System;
 
namespace ConsoleApplication
{
    public class Worker
    {
        private float psalary;
 
        public Worker(int global_salary)  { // constructor 1 
            psalary = global_salary;
        }
 
        public Worker(float day_salary , int days) { // constructor 2
            psalary = day_salary * days;
        }
 
        public void show() {
            Console.WriteLine("Salary : {0}", psalary);
        }
    }
    
    public class Owner : Worker
    {
        public Owner(int global_salary) : base(global_salary) { // constructor
            Console.WriteLine("Owner constructor");
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            try {
                Owner o = new Owner(123847);
                o.show();
            }
            catch (Exception ex) {
                Console.WriteLine(ex.Message);
            }
        }
    }
}
 
 
 
/*
run:
      
Owner constructor
Salary : 123847
    
*/

 



answered Sep 7, 2015 by avibootz
edited May 4, 2024 by avibootz
0 votes
using System;
 
class Test {
    int _age;
    
    public Test(int age) { // constructor 
        this._age = age;
    }
    
    public void Show() {
        Console.WriteLine(_age);
    }
}
 
class Program {
    static void Main() {
        Test t1 = new Test(45); // activate constructor
        t1.Show();
         
        Test t2 = new Test(53); // // activate constructor
        t2.Show();
    }
}
 
 
 
/*
run:
 
45
53
 
*/

 



answered May 4, 2024 by avibootz

Related questions

1 answer 118 views
1 answer 121 views
1 answer 116 views
116 views asked Sep 9, 2020 by avibootz
1 answer 98 views
98 views asked Sep 9, 2020 by avibootz
1 answer 117 views
117 views asked Sep 8, 2020 by avibootz
1 answer 111 views
111 views asked Sep 8, 2020 by avibootz
...