xxxxxxxxxx
public class MyClass
{
private int _myProperty;
public MyClass(int value)
{
_myProperty = value;
}
public int MyProperty
{
get { return _myProperty; }
set { _myProperty = value; }
}
}
//The MyClass class also has a public property called MyProperty that allows access to the private _myProperty field. The property has both a get and set accessor, allowing the value of the property to be read and modified.
// create an instance of MyClass and set its MyProperty value using the constructor, you can use the following code:
var obj = new MyClass(42);
Console.WriteLine(obj.MyProperty); // prints 42
In this code, a new instance of MyClass is created with a value of 42 passed to the constructor. The value of MyProperty is then accessed and printed to the console.
xxxxxxxxxx
internal class User
{
private string name;
private int age;
//constructor
public User(string name, int age)
{
this.name = name;
this.age = age;
}
//getters
public String GetName() { return name; }
public int GetAge() { return age; }
}
//////main class//////
internal class Program
{
static void Main(string[] args)
{
Program program = new Program();
program.PrintAge();
}
void PrintAge()
{
User me = new User("The Bad Programmer", 17);
Console.WriteLine("\nmy name is " + me.GetName() + " and i am " + me.GetAge() +" yo");
}
}
xxxxxxxxxx
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;
}
}
xxxxxxxxxx
public class MyClass
{
// Constructor
public MyClass()
{
// Initialization code goes here
}
// Other members and methods of the class
}
xxxxxxxxxx
public string title; //New File
public string author;
public int pages;
public Book(string aTitle, string aAuthor, int aPages)
{
title = aTitle;
author = aAuthor;
pages = aPages;
}
//Original File
Book book1 = new Book("Harry Potter", "JK Rowling", 400);
Book book2 = new Book("Lord of The Rings", "Tolkein", 700);
book2.title = "The Hobbit"; //can change the values
Console.WriteLine(book2.title);
Console.ReadLine();