Steadhaven
Steadhaven
Explore posts from servers
CC#
Created by Steadhaven on 5/13/2024 in #help
Translating basic UML diagram into code?
No description
4 replies
CC#
Created by Steadhaven on 3/4/2024 in #help
No Main and class confusion in newer C# versions
I am confused about the newer C# versions... When I make a console app, it just starts like so:
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
As you see, theres no class or Main method. In Java, and old C# if I am correct, we always have to have a class with a main method for our code to run. However, it seems that newer C# versions doesn't require this. Yet my confusion is: - OOP is rather valuable in a lot of scenarios, so what do we do without classes? - Is the Main method just in the first file we create? or where is it? it seems to run without it
52 replies
CC#
Created by Steadhaven on 3/4/2024 in #help
Which of these two books would you recommend to dive deep into?
Hi, I know other languages like Java, Scala, and some web dev like React/Next.js/TypeScript. I am now in the pursuit of learning C#. I prefer to read books as its much easier and faster for me than a computer screen, also I can more readily take notes inside the book (yeah I know some hate it). I found these two modern and updated books: - Essential C# 12.0 by Mark Michaelis: https://www.amazon.com/Essential-12-0-Addison-Wesley-Microsoft-Technology/dp/0138219516 It doesn't have ratings yet because its so new, but the old one in its series got 4.7 rating score from 15 ratings: https://www.amazon.com/Essential-C-8-0-Mark-Michaelis-ebook/dp/B08Q84TH84 - C# 12 and .NET 8 – Modern Cross-Platform Development Fundamentals: Start building websites and services with ASP.NET Core 8, Blazor, and EF Core 8 by Mark J. Price: https://www.amazon.com/12-NET-Cross-Platform-Development-Fundamentals/dp/1837635870 As you see it also has 4.7 rating score from 51 ratings, but its been out 1 month more.
5 replies
CC#
Created by Steadhaven on 2/28/2024 in #help
How to refactor lots of overloaded methods more smartly?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MethodOverload
{
internal class Program
{
static void Main(string[] args)
{
var person1 = new PersonModel("Peter", "Wow","[email protected]");
person1.GenerateEmail("gmail.com", true);
Console.WriteLine(person1.Email);
Console.ReadLine();
}
}

public class PersonModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public PersonModel()
{
//method overloading

}
public PersonModel(string firstName, string lastName, string email)
{
FirstName = firstName;
LastName = lastName;
Email = email;
}

public void GenerateEmail()
{
Email = $"{FirstName}.{LastName}@aol.com";
}
public void GenerateEmail(string domain)
{
Email = $"{FirstName}.{LastName}@{domain}";
}

public void GenerateEmail(bool firstInitialMethod)
{
if (firstInitialMethod == true)
{
Email = $"{FirstName.Substring(0,1)}{LastName}@aol.com";
}
else
{
Email = $"{FirstName}.{LastName}@aol.com";
}
}

public void GenerateEmail(string domain, bool firstInitialMethod)
{
if (firstInitialMethod == true)
{
Email = $"{FirstName.Substring(0, 1)}{LastName}@{domain}";
}
else
{
Email = $"{FirstName}.{LastName}@{domain}";
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MethodOverload
{
internal class Program
{
static void Main(string[] args)
{
var person1 = new PersonModel("Peter", "Wow","[email protected]");
person1.GenerateEmail("gmail.com", true);
Console.WriteLine(person1.Email);
Console.ReadLine();
}
}

public class PersonModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public PersonModel()
{
//method overloading

}
public PersonModel(string firstName, string lastName, string email)
{
FirstName = firstName;
LastName = lastName;
Email = email;
}

public void GenerateEmail()
{
Email = $"{FirstName}.{LastName}@aol.com";
}
public void GenerateEmail(string domain)
{
Email = $"{FirstName}.{LastName}@{domain}";
}

public void GenerateEmail(bool firstInitialMethod)
{
if (firstInitialMethod == true)
{
Email = $"{FirstName.Substring(0,1)}{LastName}@aol.com";
}
else
{
Email = $"{FirstName}.{LastName}@aol.com";
}
}

public void GenerateEmail(string domain, bool firstInitialMethod)
{
if (firstInitialMethod == true)
{
Email = $"{FirstName.Substring(0, 1)}{LastName}@{domain}";
}
else
{
Email = $"{FirstName}.{LastName}@{domain}";
}
}
}
}
9 replies
CC#
Created by Steadhaven on 2/18/2024 in #help
IDisposable and using keyword?
using (Wolf wolf = new Wolf()) {}

public interface ICarnivore {}

public class Wolf : ICarnivore, IDisposable {
public void Dispose() {}
}
using (Wolf wolf = new Wolf()) {}

public interface ICarnivore {}

public class Wolf : ICarnivore, IDisposable {
public void Dispose() {}
}
I am trying to understand using and IDisposable. It seems that we can attach IDisposable to any class, which gives us a method called Dispose that we have to implement. This dispose method is automatically run and does whatever is specified, whenever we shutdown/remove a class. And using is something we use for IDisposable classes only it seems. And somehow the using keyword would then know what to do when we shutdown the class... 1- I can kind of understand IDisposable, but want to know if my explanation is correct 2- What does it mean that a class is shutdown/removed? how can I do this in code? 3- the using keyword (as I tried to explain above) still make almost no sense, and I am even sensing it can be used without IDisposable (If needed for other language examples then I know Java/Scala/React a bit, but prefer C# explanation :) )
6 replies
CC#
Created by Steadhaven on 1/30/2024 in #help
Do while with int.TryParse - how?
I am trying to create a C-sharp program, that ask the user to pick a number between 1 and 3. It will then use that number to print one of three names I have in an array. This is how far I got:
string[] names = new string[] { "Peter", "John", "Paul" };

Console.Write("Select a number from 1-3: ");
string numberText = Console.ReadLine();

bool isValidNum = false;
int.TryParse(numberText, out int num)

do () {

} while (isValidNum == false);
string[] names = new string[] { "Peter", "John", "Paul" };

Console.Write("Select a number from 1-3: ");
string numberText = Console.ReadLine();

bool isValidNum = false;
int.TryParse(numberText, out int num)

do () {

} while (isValidNum == false);
I wanted the do-while to continue until the nubmerText is an actual number I can parse to Int, but also then be a valid number from 1-3 but I am kind of confused as to how to proceed.
35 replies
CC#
Created by Steadhaven on 1/30/2024 in #help
Copy lines shortcut not working in Visual Studio 2022
No description
1 replies