mellowzippy
mellowzippy
CC#
Created by mellowzippy on 5/27/2024 in #help
✅ Readonly methods
My VSCode is telling me to change my methods to readonly. I can't find a good explanation anywhere as to what this does and why it's recommended. So I'd really like an explanation from someone who knows.
public struct Note(Book book, List<Entry> entries) : IEnumerable<Entry>
{
public Book book = book;
public List<Entry> entries = entries;

public Note(Book book) : this(book, []) { }

public Note(Book book, Entry entry) : this(book, [entry]) { }

public readonly void Add(Entry entry)
{
entries.Add(entry);
}

public readonly void Remove(string title)
{
foreach (Entry entry in entries)
{
if (entry.Title == title)
{
entries.Remove(entry);
}
}
}

public IEnumerator<Entry> GetEnumerator()
{
return entries.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
public struct Note(Book book, List<Entry> entries) : IEnumerable<Entry>
{
public Book book = book;
public List<Entry> entries = entries;

public Note(Book book) : this(book, []) { }

public Note(Book book, Entry entry) : this(book, [entry]) { }

public readonly void Add(Entry entry)
{
entries.Add(entry);
}

public readonly void Remove(string title)
{
foreach (Entry entry in entries)
{
if (entry.Title == title)
{
entries.Remove(entry);
}
}
}

public IEnumerator<Entry> GetEnumerator()
{
return entries.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
10 replies
CC#
Created by mellowzippy on 4/23/2024 in #help
✅ Mysterious byte problem
No description
45 replies
CC#
Created by mellowzippy on 1/29/2024 in #help
Having trouble understanding patterns
Hello, I`m a second year student and I'm reading about patterns like a factory pattern. I have trouble understanding why I should use patterns and when to use which pattern.
254 replies
CC#
Created by mellowzippy on 2/13/2023 in #help
❔ ✅ Trouble printing values
class Program
{
public static void Main()
{
var square = new Square(5);
Console.Write($"Side: {square.Side}");
Console.WriteLine($"Area: {square.Area()}");
Console.WriteLine($"Perimeter: {square.Perimeter()}");

var square2 = new Square(10);
Console.Write($"Side: {square2.Side}");
Console.WriteLine($"Area: {square2.Area()}");
Console.WriteLine($"Perimeter: {square2.Perimeter()}");
}
}

class Square
{
public int Side;

public Square(int side)
{ this.Side = side; }

public int Area() => this.Side * this.Side;
public int Perimeter() => this.Side * 4;
}
class Program
{
public static void Main()
{
var square = new Square(5);
Console.Write($"Side: {square.Side}");
Console.WriteLine($"Area: {square.Area()}");
Console.WriteLine($"Perimeter: {square.Perimeter()}");

var square2 = new Square(10);
Console.Write($"Side: {square2.Side}");
Console.WriteLine($"Area: {square2.Area()}");
Console.WriteLine($"Perimeter: {square2.Perimeter()}");
}
}

class Square
{
public int Side;

public Square(int side)
{ this.Side = side; }

public int Area() => this.Side * this.Side;
public int Perimeter() => this.Side * 4;
}
Expected output:
Side: 5
Area: 25
Perimeter: 20
Side: 10
Area: 100
Perimeter: 40
Side: 5
Area: 25
Perimeter: 20
Side: 10
Area: 100
Perimeter: 40
Actual output:
Side: 5Area: 25
Perimeter: 20
Side: 10Area: 100
Perimeter: 40
Side: 5Area: 25
Perimeter: 20
Side: 10Area: 100
Perimeter: 40
How do I get it to print under each other and not next to each other?
7 replies