C
C#15mo ago
kuulie

✅ 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
Pobiega
Pobiega15mo ago
no the class is internal, and the field belongs to an instance of the class
kuulie
kuulieOP15mo ago
oh so then capping does exist ChatGPT is wrong then
Pobiega
Pobiega15mo ago
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.
kuulie
kuulieOP15mo ago
haha text generator is a good way to look at it
Pobiega
Pobiega15mo ago
its literally what it is. it can seem really smart at times
kuulie
kuulieOP15mo ago
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
Pobiega
Pobiega15mo ago
so feel free to ask it creative questions, but dont ask for facts
mtreit
mtreit15mo ago
Technically any field can be accessed by another assembly (using reflection) but I'm sure that's outside the spirit of the question.
Pobiega
Pobiega15mo ago
yep, I assumed no use of reflection 🙂
kuulie
kuulieOP15mo ago
too noob to know that class C { public void Foo() {} }
Pobiega
Pobiega15mo ago
also, explicit over implicit always specify your access modifiers 🙂
kuulie
kuulieOP15mo ago
Foo is internal?
Pobiega
Pobiega15mo ago
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
kuulie
kuulieOP15mo ago
ohh right. i can't even access Foo unless I create an instance. and the class is again, internal, by default
Pobiega
Pobiega15mo ago
yep
kuulie
kuulieOP15mo ago
got it. thanks!
Pobiega
Pobiega15mo ago
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
Angius
Angius15mo ago
$tias when
Pobiega
Pobiega15mo ago
but I'm lazy
kuulie
kuulieOP15mo ago
ok thanks guys, how do i close this chat?
Pobiega
Pobiega15mo ago
$close
MODiX
MODiX15mo ago
Use the /close command to mark a forum thread as answered
Want results from more Discord servers?
Add your server