CSharp - Write program to Create class and add a field

Requirements

  • Create a class called ClassEx1.
  • encapsulate only one integer field, MyInt, into it.
  • initialize the value 25 into that field.
  • create two objects-obA and obB from our ClassEx1 class.
  • output the values of the variable MyInt inside the objects.

Hint

You can use the following structure and fill in your code.

using System; 

class Program 
{ 
        static void Main(string[] args) 
        { 

        } 
} 

Demo

using System;

class ClassEx1/* w  w w.j  a  v a2  s  . c  o m*/
{
    public int MyInt = 25;
}
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("A class demo with 2 objects");
        ClassEx1 obA = new ClassEx1();
        ClassEx1 obB = new ClassEx1();
        Console.WriteLine("obA.i ={0}", obA.MyInt);
        Console.WriteLine("obB.i ={0}", obB.MyInt);
    }
}

Result

Related Topic