Answers for "example of constructor in c#"

C#
18

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.
}
Posted by: Guest on July-02-2020
3

c# class constructor

//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!
Posted by: Guest on August-04-2020
1

Constructor in C#

class Car
    {
        public Car(string Name, string Model, int Seat_Number, int Door_Number, double Price)
        {
            name = Name;
            model = Model;
            seat_Number = Seat_Number;
            door_Number = Door_Number;
            price = Price;
        }
 }
Posted by: Guest on January-29-2021
0

example of constructor in c#

class Program
{
  static void Main(string[] args)
  {
    Car Ford = new Car("Mustang", "Red", 1969);
    Car Opel = new Car("Astra", "White", 2005);

    Console.WriteLine(Ford.model);
    Console.WriteLine(Opel.model);
  }
}
Posted by: Guest on May-06-2021

C# Answers by Framework

Browse Popular Code Answers by Language