static void Main(string[] args) { /*Access Modifiers*/ // -See Primitive Types Projects which is referencing this project to verify the examples PublicClass publicClass = new PublicClass(); //You can access a public class from anywhere, even outside the assembly InternalClass internalClass = new InternalClass(); //You can access an internal class from anywhere inside this assembly InternalClassChild internalClassChild = new InternalClassChild(); //internalClass.ProtectedMethod(); -Impossible since we can't invoke protected methods outside the class and it's children internalClassChild.InvokeParentProtectedMehtod(); //Protected method invoked through's child's public method //publicClass.PrivateMethod(); -Impossible since we can't invoke private method's outside a class publicClass.PublicMethod(); //Acessing the private method through a public method, helpful when performing Encapsulation //AbtractClass abtractClass = new AbtractClass(); -Impossible since we can't instanciate Abtract classes AbstractClass abtractClass = new ImplementingClass(); //ImplementingClass imlpements every method fromo Abstract class abtractClass.AbtractMethod(); //Virtual method that shares functionality between the base class and the child's class internalClassChild.VirtualMethod(); Console.WriteLine(StaticClass.StaticString); //For static classes, you do not need to create an instance, you can invoke the methods directly SealedClass sealedClass = new SealedClass(); //You can create instances of a sealed class, but you can't inherit from it Console.ReadLine(); }
static void Main(string[] args) { //Integral Types // If the value represented by an integer literal exceeds the range of Type, a compilation error will occur, Try it!. sbyte signedSbyteNeg = -128; byte unsignedByte = 255; char character = 'a'; short shortNumber = 32767; int integer = 2147483647; uint unsignedInteger = 4294967295; long longNumber = 9223372036854775807; var varWhichIsInt = 3; Console.WriteLine(signedSbyteNeg.GetType().ToString()); Console.WriteLine(varWhichIsInt.GetType().ToString()); // If the value exceds at runtime sbyte signedSbyte = 127; signedSbyte++; Console.WriteLine(signedSbyte); //You get -128 since C# uses the two's complement's system //Floating-point Types double doubleNumber = 3.5; float floatNumber = 3.5F; //Need to add the F to force the double number into a float Console.WriteLine(doubleNumber); Console.WriteLine(floatNumber); //DateTime DateTime dateTime = new DateTime(1987,8,24); Console.WriteLine(dateTime.ToShortDateString()); Console.ReadLine(); //AccessModifiers example PublicClass publicClass = new PublicClass(); //possible since the class is public //InternalClass internalClass = new InternalClass(); *Impossible since Internal only works on the current assembly }
static void Main(string[] args) { PublicClass pub = new PublicClass(); PublicClassPrivateProp pubpriv = new PublicClassPrivateProp(); PublicClassGetSet pubgetset = new PublicClassGetSet(); OuterClass outer = new OuterClass(); OuterClass.TestProtected outerprotectedtest = new OuterClass.TestProtected(); OuterProtectedTestClass outerprotectedderivedtest = new OuterProtectedTestClass(); // Access the public property of the PublicClass class pub.PublicProperty = "setting public property to value"; Console.WriteLine(pub.PublicProperty); // Try to access the private property of the PublicClassPrivateProp class // pubpriv // Access the properties with get and set from the PublicClassGetSet class pubgetset.OnlySet = "new value 1"; Console.WriteLine(pubgetset.OnlyGet); pubgetset.Both = "new value 2"; Console.WriteLine(pubgetset.Both); // Try to access the private classes nested inside the outer class //outer // Access the protected OuterClassProtected class from class within OuterClass Console.WriteLine(outerprotectedtest.GetString()); // Use a derived class to access the protected OuterClassProtected class, this class is outside the OuterClass Console.WriteLine(outerprotectedderivedtest.GetString()); Console.WriteLine("Press ENTER to exit"); Console.ReadLine(); }