Answers for "c# object"

C#
3

how to make a object in c#

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }
    // Other properties, methods, events...
}

class Program
{
    static void Main()
    {
        Person person1 = new Person("Leopold", 6);
        Console.WriteLine("person1 Name = {0} Age = {1}", person1.Name, person1.Age);

        // Declare new person, assign person1 to it.
        Person person2 = person1;

        // Change the name of person2, and person1 also changes.
        person2.Name = "Molly";
        person2.Age = 16;

        Console.WriteLine("person2 Name = {0} Age = {1}", person2.Name, person2.Age);
        Console.WriteLine("person1 Name = {0} Age = {1}", person1.Name, person1.Age);

        // Keep the console open in debug mode.
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}
/*
    Output:
    person1 Name = Leopold Age = 6
    person2 Name = Molly Age = 16
    person1 Name = Molly Age = 16
*/
Posted by: Guest on August-06-2020
1

classes c#

public class x
    {
		(condition)
    }
Posted by: Guest on December-09-2020
0

c# objects

using System;

class Book
  {
    public string title;
    public string author;
    public int pages;
  }


class MainClass {
  public static void Main (string[] args) {
    Book book1 = new Book();
    book1.title = "Harry Potter";
    book1.author = "JK Rowling";
    book1.pages = 400;
    Console.WriteLine(book1.title);
  }
}
Posted by: Guest on March-17-2021
1

c# object

// Create a object
class Car 
{
  string color = "red";

  static void Main(string[] args)
  {
    Car myObj = new Car();
    Console.WriteLine(myObj.color);
  }
}
Posted by: Guest on July-19-2021

C# Answers by Framework

Browse Popular Code Answers by Language