Divergent
0
Q:

c# constructor

public class Person
{
   private string last;
   private string first;

   public Person(string lastName, string firstName)
   {
      last = lastName;
      first = firstName;
   }

   // Remaining implementation of Person class.
}
10
class MyClass {
  public MyClass () {
    //constructor code
  }
}
11
//The Class
public class MyClass
{
  	//Some Variables
	public readonly int var1;
  	public readonly string var2;
  	//readonly only lets you set it ina constructor	
  
  	//The Constructor (public [class name](parameters))
  	public MyClass(int param1, string param2)
    {
      	//Setting the earlier variables
     	var1 = param1;
      	var2 = param2;
    }
}

//Creating an object using that class
MyClass Class1 = new MyClass(10, "Hello, World!");
MyClass Class2 = new MyClass(9999, "This is the second object!");

Console.WriteLine(Class1.var1);
Console.WriteLine(Class2.var2);

//Output
//10
//This is the second object!
2
// Create a Car class
class Car
{
  public string model;  // Create a field

  // Create a class constructor for the Car class
  public Car()
  {
    model = "Mustang"; // Set the initial value for model
  }

  static void Main(string[] args)
  {
    Car Ford = new Car();  // Create an object of the Car Class (this will call the constructor)
    Console.WriteLine(Ford.model);  // Print the value of model
  }
}

// Outputs "Mustang"
 
0
// C# program to illustrate the 
// concept of inheritance in the 
// constructor when the derived 
// class contains a constructor 
using System; 
  
// Class Tank to give the 
// dimension of the tank 
class Tank { 
  
    double t_radius; 
    double t_height; 
  
    // Properties for Radius and Height 
    public double Radius 
    { 
        get {  
               return t_radius;  
            } 
  
        set { 
               t_radius = value < 0 ? -value : value; 
            } 
    } 
  
    public double Height 
    { 
        get {  
               return t_height;  
            } 
  
        set {  
              t_height = value < 0 ? -value : value;  
            } 
    } 
  
    // Display the dimension of tanks 
    public void DisplayDimension() 
    { 
        Console.WriteLine("The radius of tank is :" + Radius  
                 + " and the height of tank is :" + Height); 
    } 
} 
  
// A derived class AreaOfTank  
// inheriting Tank Class 
class AreaOfTank : Tank { 
  
    string Color; 
  
    // Constructor 
    public AreaOfTank(string c, double r, double h) 
    { 
  
        // from base class 
        Radius = r; 
        Height = h; 
  
        // from derived class 
        Color = c; 
    } 
  
    // Return area of tank 
    public double Area() 
    { 
        return 2 * 3.14 * Radius * Height; 
    } 
  
    // Display the color of tank 
    public void DisplayColor() 
    { 
        Console.WriteLine("The Color of tank is " 
                                        + Color); 
    } 
} 
  
// Driver Class 
class GFG { 
  
    // Main Method 
    static void Main() 
    { 
  
        // Create and initialize the 
        // object of AreaOfTank 
        AreaOfTank t1 = new AreaOfTank("Green", 6.0, 12.0); 
        t1.DisplayColor(); 
        t1.DisplayDimension(); 
        Console.WriteLine("Area is " + t1.Area()); 
    } 
}
-1

New to Communities?

Join the community