How to define and use constructor in class with C#

1 Answer

0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Test
    {
        int _n;

        public Test(int n)
        {
            _n = n;
        }
        public void Show()
        {
            Console.WriteLine(_n);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Test t1 = new Test(31);
            t1.Show();

            Test t2 = new Test(983);
            t2.Show();
        }
    }
}


/*
run:
 
31
983

*/

 



answered Dec 31, 2016 by avibootz

Related questions

1 answer 203 views
1 answer 222 views
1 answer 222 views
2 answers 247 views
1 answer 160 views
1 answer 198 views
...