Pages

C# Features Through the History - Version 1.0




In almost two decades of existence, C# had delivered a huge amount of features for us, as developers, to use. More than this, C# is now one of the most used languages in the world. Now, with the .NET Core Framework, the language is getting more strength, with infinite new possibilities.

Because of this, with the idea to organize the knowledge about the language, I’d like to organize it through it’s history, in an incremental way. It’s not my idea to serve as a complete tutorial about the language but, more important, to focus on some aspect of the language that are pertinent and important for us today. 

I’m adding the example codes in this repository on GitHub. Feel free to use this project as a reference.  

How to use the examples 

The examples are available hereYou can open the solution and play around. Just uncomment the code and have fun! 

 

So, let’s get started! 


The first version of C# was released in 2002, following the design goals of ECMA-334 First Edition (2001): 
  • Simple, modern and general-purpose object-oriented language; 
  • Strong type checking, array bound checking; 
  • Automatic memory management policies implemented by a garbage collection; 
  • Use in distributed environments; 
  • Portability from other programming languages (e.g. Java, C, C++); 
  • Internationalization support; 
  • Applications to be hosted and embedded systems. 

The idea of this lecture is to be simple, and to serve as a base knowledge about the language. In this version, we where introduced with some base concepts provided by the language: 



Namespaces provide a logical organizational system. Namespaces are used both as an “internal” organization system for a program, and as an “external” organization system—a way of presenting program elements that are exposed to other programs.  

Assemblies are used for physical packaging and deployment. An assembly may contain types, the executable code used to implement these types, and references to other assemblies. 


Types: 

  • Value Typesthe variables of these types contains the data itself. Examples: simple types (e.g. chart, int, float), enum, struct;
  • Reference Typesit’s possible for two variables to reference the same object. Consequently, one operation in a variable will affect the object reference by the other variables. Examples: classes, interfaces, delegates, arrays. 
List of predefined types with examples: 
 
Remember that you can move a variable between the types, using implicit or explicit conversions. Implicit conversions are considered safe (e.g. like attribute a short variable to a int one). Explicit conversions, on the other hand, can generate data loss. In this case, if you really want to execute, you should use some syntax (e.g. int intValue = (int) longValue). 



All types derives from type object. In this case, it’s possible to convert a value type into a reference type and vice-versa. 
  • Boxing: convert a value type into a reference type. The value is stored on the heap; 
  • Unboxing: convert a reference type into a value type. The value is added on the stack. 
Both of these process are computationally expensive. Be aware about when and how to use in a properly way.
        
	int i = 123;
	object o = i; //boxing
	int x = (int) o; //unboxing

	int valueTest = 10;
	ArrayList arrayTest = new ArrayList();
	arrayTest.Add(valueTest); //boxing
	int outArray = (int)arrayTest[0]; //unboxing

	Console.ReadKey();



The manipulation of operands and operators are the bases of all languages. It was not different for C#. The list of the expressions in version 1.0, in order of precedence: 

The actions executed in a program are expressed in statements. This is a list of the statements provided by C#: 

Statement 
Description 
Example 



Declaration 
Declaration of variables or constants 

double test = 2; const double pi = 3.1415; 

Assignment of values and method invocation 
area = calculateArea(10, 12); 

Selection 

Specify conditions that will change the execution flow 
if, else, switch, case 

Interaction 

Loop thought collections and execute commands repeatedly 
do, for, foreach, in, while 

Jump 

Mecanisms to transfer control to another section of the code 
break, continue, goto, return 

Exception Handling 

Provide an action for any error that occurs at runtime 
try, catch, finally 
Verify whether numerical operations are allowed 
checked 
{ 
    int i3 = 2147483647 + ten; 
    Console.WriteLine(i3); 
} 



A class, is one of the main important components in an Objected-Oriented Programming language. It works like a blueprint, a model, for a specific object that can be created and used for the code in the future. They are reference types and composed by: 
  • Fields: variables that are declared directly in the class. They used to be private, to avoid be exposed externally; 
  • Properties:  a mechanism to provide a way to read, write or compute the value of a private field; 
  • Methods: a block of code that contains a series of statements. They can have a return type or not (void);
  • Constructor: is executed every time you create a new object, based on a class.
            
        public class ClassExamplePerson
        {
            //definition of a private fields
            private string name;
    
            //definition of property to expose the field
            public string Name
            {
                get
                {
                    return name;
                }
                set
                {
                    name = value;
                }
            }
            
            //constructor that takes no arguments
            public ClassExamplePerson()
            {
                Name = "Unknown";
            }
    
            //constructor that takes one argument
            public ClassExamplePerson(string name)
            {
                Name = name;
            }
    
            //method that returns a boolean value
            public bool HasName()
            {
                return !String.IsNullOrEmpty(this.Name);
            }
        }
    

            
                //call the constructor that has no parameters
                var person1 = new ClassExamplePerson();
                Console.WriteLine(person1.Name);
    
                //call the constructor that has one parameter
                var person2 = new ClassExamplePerson("John de Barro");
                Console.WriteLine(person2.Name);
                //string representation of the person2 instance
                Console.WriteLine(person2);
    



All types and members has an accessibility level. They are called access modifiers. Here is the list for C#: 


font: Stackoverflow 



Provides a specie of contract between and object and who is using it. A interface can contains method and properties declarations.
        
interface IAnimal
    {
        string Name { get; set; }

        bool isHuman();
    }

    public class Animal : IAnimal
    {
        private string name;
        private bool human;

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        public bool IsHuman
        {
            get
            {
                return human;
            }
            set
            {
                human = value;
            }
        }

        public bool isHuman()
        {
            return human;
        }
    }

        
            Animal animal1 = new Animal();
            animal1.Name = "Dog1";
            animal1.IsHuman = false;

            Console.WriteLine("Name: " + animal1.Name + "Is Human: " + animal1.IsHuman);




Delegates provide a late binding mechanism in .NET. It’s similar to function pointers in C++. You can define callback methods and implement event handling.
        
    public class DelegateExample
    {
        //defining a delegate type
        public delegate void Del(string message);

        //creating a method for a delegate
        public static void DelegateMethod(string message)
        {
            Console.WriteLine(message);
        }

        public static void ExecuteExample()
        {
            Console.WriteLine("C# 1.0 - Delegate Example");

            //instantiate the delegate
            Del handler = DelegateMethod;

            //call the delegate
            handler("hello!!");
        }
    }



Events 

An event is a mechanism to enable a class or object to notify other classes or objects about something that is happening. It works like a notification strategy.
        
    public delegate string MyDel(string str);
    public class EventExample
    {
        event MyDel MyEvent;

        public EventExample()
        {
            this.MyEvent += new MyDel(this.WelcomeUser);
        }

        private string WelcomeUser(string userName)
        {
            return "Welcome " + userName + "!";
        }
        public static void ExecuteExample()
        {
            Console.WriteLine("C# 1.0 - Event Handler Example");

            var obj1 = new EventExample();
            var result = obj1.MyEvent("Tutorials Point");
            Console.WriteLine(result);
        }
    }




Attributes provide a powerful method of associating metadata, or declarative information, with code (assemblies, types, methods, properties, and so forth). After an attribute is associated with a program entity, the attribute can be queried at run time by using a technique called reflection. 

    [Serializable]                  //attribute that indicates that the class can be serialized
    [Custom("Just a test name")]    //using a custom attribute    
    public class AttributeExample
    {
        public static void ExecuteExample()
        {
            Console.WriteLine("C# 1.0 - Atribute Example");
            CustomAttribute attrib = new CustomAttribute("name");
        }
    }

    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
    public class CustomAttribute : Attribute //define a custom atribute
    {
        private string name;

        public CustomAttribute(string name)
        {
            this.name = name;
        }
    }

They are so many features, specially in the first version of this language. However, the main idea is to stress the main pertinent ones present in this version. They are the important blocks o the language, and we can continue iterate from here through the next versions.

Fabio Ono

1 comment:

  1. Interesting article , the main features of one of the most used programming languages in the world in its first version.

    ReplyDelete