C
C#15mo 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
x0rld15mo ago
by default it's internal
Ronnie
Ronnie15mo ago
ik
mtreit
mtreit15mo 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
Ronnie15mo 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
mtreit15mo ago
Sure If the public class is not marked sealed
Ronnie
Ronnie15mo ago
but what about assemblies? i dont know much about them
mtreit
mtreit15mo ago
The internal keyword restricts access to code in the same assembly
Ronnie
Ronnie15mo 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
mtreit15mo 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
Ronnie15mo 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
mtreit15mo ago
I'm not sure what that has to do with multiple assemblies
Ronnie
Ronnie15mo ago
public classes can be assessed by different assemblies within the same solution, no?
Ronnie
Ronnie15mo ago
mtreit
mtreit15mo ago
If there is a project reference
Ronnie
Ronnie15mo ago
yeah i just made one now lol
Thinker
Thinker15mo 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
Ronnie15mo 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
Thinker15mo ago
Right click on the Dependencies in project 2, then add project reference (I think it's called that), then select project 1
Ronnie
Ronnie15mo ago
ive done that but how do i use the public class in the program now?
Thinker
Thinker15mo ago
Add a using CSharp; at the top of your file
Ronnie
Ronnie15mo ago
Ronnie
Ronnie15mo ago
oo no errors yet hold on tho oml it worked all of this just to figure out how public classes work 😮‍💨
Accord
Accord15mo 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.