C class creation and initialization instance resolution

  • 2020-06-19 11:39:00
  • OfStack

In this paper, a simple example is used to realize the creation and initialization of classes. The implementation code is as follows:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace C_program_test
{
  class Person
  {
    public string Name; // because Name There is no assignment, and it is string Type, so its default value is Null
    public int Age; // because Age and Gender There's no assignment, and it's two int Type, so the default value for both of them is 0
    public int Gender;
 
    public void sayHello()
    {
      Console.WriteLine(" Hello, everyone ");
      Console.ReadKey();
    }
  }
  class Program
  {
    static void Main(string[] args)
    {
      //int i = 1;//  Value type initialization is a direct assignment. Reference type initialization is required new 
      Person p1 = new Person(); //new Person() Is to create 1 a Person Class objects. Person p1 = new Person() That means create it first 1 a Person Object of type   And then using variables p1 Pointing to it 
      p1.sayHello();
       
    }
  }
}

Related articles: