I just got started today on trying to learn c# again and started researching a few things and looking at some dll files I opened. One thing I saw that intrigued me was a protect command. I researched the function and assume it’s to obscure code obviously.
That being said, how does one use this function to protect their own source code and how strong is it? Would it be beneficial to protect in another way as well or is this just it of the realm for most? I was combing through a unity dll file trying to learn a little when I saw this.
It was nice to see the usage vs what I’ve seen in basic c# logic courses which is about the level I’m at. I have another question regarding local and global variables but I have to dig to remember.
protected
is an access modifier. If you’re really new, this might not make a lot of sense, but most of it only makes sense after you’ve heard a lot of it.
In early programming languages, we didn’t have any concept of “modules”. We had either memory addresses for lines of code or line numbers to refer to them, and we used GOTO or jump instructions to get there. This made organization quite hard, because any code could “see” all variables in the program so it was very hard to keep track of which parts of the code could change things.
Later languages started adding ways to treat individual parts of your code as if they were separate, which let us name those parts of the code instead of using line numbers or memory addresses. Further, it let us say that some variables “belonged” to these named parts of the code, and that things outside of these separate parts were not allowed to manipulate those variables.
C# is an OOP language, which means the way we separate our code into pieces involves making “classes” that represent objects. Classes have “members” that represent the data (properties and fields) and actions (methods) a class can perform. But sometimes, we don’t want the entire world to be able to see all of the data related to a class. For example, you’re a person, and you have a real name, but you probably don’t want me to see that real name and I don’t have a good reason to be able to see it. So that “property” of you is a private piece of information.
What we call “access modifiers” in C# are words that let us say data or actions of a class are more or less public than each other. The ones you will see the most are:
public
: this means ANYTHING can see this data or call this method. Your reddit username is public information.private
: This means ONLY this type can see the data or call the method. You probably have a bank account number and a lot of other bits of completely private information you protect.protected
: This means only this type and derived types can see the data or call the method. This is tough for real-world analogies. It’s like a family secret recipe. Family members will share it with each other, but if I ask you for it I’ll be denied.
There are some other modifiers, but they are for more exotic scenarios that are even harder to describe with real-world analogies and thus not likely to be vital to your knowledge at this stage.
THIS!!! 🤯 Thank you for this much needed explanation. When I read about some of this stuff on my own and through some of the books I have it absolutely makes no sense. This put me in line for the most part on what it actually is. That being said the furthest I got was basic if, then, else, else if, arrays, etc… Iirc it was mostly in visual studio on blackboard through devry university when I was looking at one of their electronics programs.
I haven’t touched it though since 2014. That being said I was poking around to get familiar again and to continue the learning process. I have a feeling I may be on here and stack exchange for some help fairly frequently.
Not sure what do you mean by “protect” but “protected” is a member access modifier, google it up you might find it useful, but for a beginner I would recommend not to bother with advanced OOP designs and more about learning basics.
That’s what it was! I couldn’t find much looking for it which was why I asked. I gotcha though, I’ll continue trying to read about it. I appreciate your help and yes, I do still need to focus on basics for sure.
First, what do you mean “looking at some dll files I opened”? Do you mean, you used a decompiler to get C# code from IL byte code? In that case, you are looking at an approximation of the code, not the actual source. If this is a .NET Core library or other open-source library, a better option might be to look at the actual source on GitHub.
Secondly, can you give us some context on what you mean by “protect command” (such as by copying and posting three surrounding lines)? When you say “function”, we hear “method”, which would require a full class name to make sense. You may be talking about the protected
access modifier (which protects developers from things they don’t need to know about), or you may be talking about DataProtection.Protect
, which protects user data. Whatever the case is, it is almost certainly not there to protect source code.
This was what I was looking at, I was using dnspy to look at the code. The first person that answered explained what I was looking at I believe.
picture of code