C
C#2y ago
Ronnie

❔ Whats the difference between classes and public classes?

Tried looking at Microsoft docs earlier but the don't really answer the question. I already know what assess modifiers are, but how to do they affect classes?
23 Replies
x0rld 👻 🎃
by default it's internal
Ronnie
RonnieOP2y ago
ik
mtreit
mtreit2y ago
It effects whether other code can make use of the class. For instance, if you make a class library and publish it...and I consume that class library...my code can only use public classes In that library
Ronnie
RonnieOP2y ago
so what if i had 2 different classes in 2 different files in the same project, can the normal class inherit from the public one
mtreit
mtreit2y ago
Sure If the public class is not marked sealed
Ronnie
RonnieOP2y ago
but what about assemblies? i dont know much about them
mtreit
mtreit2y ago
The internal keyword restricts access to code in the same assembly
Ronnie
RonnieOP2y ago
oh wait when using public classes from another assembly do you need to use refrences im not sure how to add one in visual studio
mtreit
mtreit2y ago
Yes, you need some kind of reference to that assembly...usually a PackageReference for code from some third party or ProjectReference for code you own
Ronnie
RonnieOP2y ago
oh ok do projects usually need multiple assemblies? because from what i know you cant use private/ private protected acsess modifiers within a namespace
mtreit
mtreit2y ago
I'm not sure what that has to do with multiple assemblies
Ronnie
RonnieOP2y ago
public classes can be assessed by different assemblies within the same solution, no?
Ronnie
RonnieOP2y ago
mtreit
mtreit2y ago
If there is a project reference
Ronnie
RonnieOP2y ago
yeah i just made one now lol
Thinker
Thinker2y ago
Just a note about terminology, a solution contains multiple projects, each project is one assembly, and each assembly contains multiple types (like classes)
Ronnie
RonnieOP2y ago
im struggling to create a refrence from the program in assembly 1 to the other program in assembly 2 by any chance do you know how to do this im trying to use the public class within the program in assembly 1 in the program in assembly 2
using System;
using System.Collections.Generic;

namespace CSharp
{
public class Foo
{
public void Method1()
{
Console.WriteLine("This comes from Program 1");
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(" P1 Running.");
}
}
}
using System;
using System.Collections.Generic;

namespace CSharp
{
public class Foo
{
public void Method1()
{
Console.WriteLine("This comes from Program 1");
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(" P1 Running.");
}
}
}
^assembly#1
Thinker
Thinker2y ago
Right click on the Dependencies in project 2, then add project reference (I think it's called that), then select project 1
Ronnie
RonnieOP2y ago
ive done that but how do i use the public class in the program now?
Thinker
Thinker2y ago
Add a using CSharp; at the top of your file
Ronnie
RonnieOP2y ago
Ronnie
RonnieOP2y ago
oo no errors yet hold on tho oml it worked all of this just to figure out how public classes work 😮‍💨
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server