✅ Question about access modifiers
If I have a class (internal by default) and a field that is public, can that field be accessed by other assemblies?
23 Replies
no
the class is internal, and the field belongs to an instance of the class
oh so then capping does exist
ChatGPT is wrong then
oh boi
you triggered my trap card
Chat GPT is a text generator.
its not an intelligence, it cant think, it doesnt know facts. It just generates text.
haha text generator is a good way to look at it
its literally what it is. it can seem really smart at times
i'm reading a book on C# and a section mentioned accessibility capping. then when i asked ChaptGPT it gave me the opposite answer so i wanted to ask here to make sure
so feel free to ask it creative questions, but dont ask for facts
Technically any field can be accessed by another assembly (using reflection) but I'm sure that's outside the spirit of the question.
yep, I assumed no use of reflection 🙂
too noob to know that
class C { public void Foo() {} }
also, explicit over implicit
always specify your access modifiers 🙂
Foo is internal?
effectively, since you can't really use the class unless you know it exists
If you use reflection to inspect it, I'm sure it will count as public
but since the type itself isnt, its kind of a moot point
ohh right. i can't even access Foo unless I create an instance. and the class is again, internal, by default
yep
got it. thanks!
I'm semi-curious as to what would happend if you create it internally, expose it as
dynamic
and try to access the public field
but purely from a "hmm what if" scenario, please NEVER EVER do this in real code$tias
but I'm lazy
ok thanks guys, how do i close this chat?
$close
Use the /close command to mark a forum thread as answered