Posts Tagged Under: .net

c# – Create a read-only property (aka a constant)

So far we have looked at 5 ways to store data in a class’s object (aka an instance) in the form of “properties”. Of which one of the recommended ways of setting values to properties, is by using the constructor method, e.g.:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class Employee
{
    public string Firstname { get; set; }
    public int Age { get; set; }
    public double Salary { get; set; }

    public Employee(string firstname, int age, double salary)
    {

        Firstname = firstname;
        Age = age;
        Salary = salary;
  															

c# – Inheritence in Action

Lets say we have a supermarket and it sells a different types of fruits, then you could create a class for each type of fruit:

The above will output:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InheritenceInAction
{
    class Program
    {
        static void Main(string[] args)
        {
			// Here we create an object using the orange class
            Orange AnOrange = new Orange(1.25, 45, "Spain", "25-01-2015", "sweet", true, "clementine");
			string ContainsSeedsSentence;
            if (AnOrange.ContainsSeeds)
            {
     															

c# – Method Overloading: Have two or methods of the same name

Method Overloading is basically a technique that lets you have different versions of the same method, where each version of the method does a different thing.

It essentially makes methods more versatile.

Each version of the method accepts different/types of input parameters.

The way you control which of version method you want to trigger, is by specifying the correct number/type of input parameters for that corresponding method.

In the following example, we have 2 methods, which in this case achieves the same goal by different means. The here is to instantiate an object:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class Employee
{
    public string Firstname { get; set; }
    public int Age { get; set; }
    public double Salary { get;															

c# – Creating objects from generic parent classes

In the previous unit we came across:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InheritenceInAction
{
    class Program
    {
        static void Main(string[] args)
        {
            // Here we create an object using the orange class
            Orange AnOrange = new Orange(1.25, 45, "Spain", "25-01-2015", "sweet", true, "clementine");
            string ContainsSeedsSentence;
            if (AnOrange.ContainsSeeds)
            {
  															

c# – What are Variables Types

A variable is essentially a container that’s used for storing data. This container has a name assigned to it. In programming, this name is used to refer to the variable’s content.

In c# there are actually three high-level types of variables:

  • Value Types
  • Reference types
  • Pointer types (we will ignore this for now, but will cover it later)

Value Types

The following variable types fall under this category:

Type Represents Range Default
Value
bool Boolean value True or False False
byte 8-bit unsigned integer 0 to 255 0
char 16-bit Unicode character U +0000 to U +ffff ‘\0’
decimal 128-bit precise decimal values with 28-29 significant digits (-7.9 x 1028 to 7.9 x 1028) / 100 to 28 0.0M
double 64-bit double-precision floating point type (+/-)5.0 x 10-324 to (+/-)1.7 x 10308 0.0D
float 32-bit single-precision floating point type -3.4 x 1038 to + 3.4 x 1038 0.0F
int 32-bit signed integer type -2,147,483,648 to 2,147,483,647 0
long 64-bit signed integer type -923,372,036,854,775,808 to 9,223,372,036,854,775,807 0L
sbyte 8-bit signed integer type -128 to

c# – Over-riding generic methods

Child classes can inherit methods from it’s parent class, in the same way that it inherits properties. Here is an example of the Orange class inheriting the “SomeInfo” method, which have been originally defined in the Item class:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InheritenceInAction
{
    class Program
    {
        static void Main(string[] args)
        {
            Orange AnOrange = new Orange(1.25, 45, "Spain", "25-01-2015", "sweet", true, "clementine");

            // The "SomeInfo" method hasn't been explicitly defined in the Orange class, 
			// Howeever the following method call															

c# – An overview of commonly used variables

Previously we looked at why variables are categorized as either “value types”, or “reference types” (primarily for memory reasons).

Now lets take a look at these variables in action, starting with the string variable.

String variables

We already encountered string variables when we created the “message” string variable in our hello world program.

You can also Concatenate existing string variables to form new string variables:

using System;
class ConcatenateStrings
{
	static void Main()
	{
		string firstname = "James" ;
		string lastname = "Bond" ;
		string fullname = firstname + " " + lastname;
		Console.WriteLine ("My name is " + lastname + ", " + fullname );

                string country = "uk";
            															

c# – Random Number Generator

Here’s a quick a simple class for creating a a series of random numbers, by creating an instance of the “Random” class, and applying this class’s next() method:

http://msdn.microsoft.com/en-us/library/system.random%28v=vs.110%29.aspx

Here is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RandomNumberGenerator
{
    class Program
    {
        static void Main(string[] args)
        {
			Random RandomNumber = new Random();
			
			Console.WriteLine("Here is a list of random numbers:");
			Console.WriteLine(RandomNumber.Next());
			Console.WriteLine(RandomNumber.Next());
			Console.WriteLine(RandomNumber.Next());
			Console.WriteLine(RandomNumber.Next());
			Console.WriteLine(RandomNumber.Next());
			
		}
    }
	
}

c# – Static and Non-Static Methods

C# is a object oriented language, and that means that whenever we declare a new variable, we are actually using a class that generates (aka instantiates) that variable for us. Let’s says that somewhere in our program we have the following statement:

int MyFavouriteNumber = 7 ;

Here we are creating a integer variable called MyFavouriteNumber which has the value 7.

The word “int” is actually a c# builtin keyword, When using the int keyword before a variable’s name, we are essentially saying that we want this variable instantiated using the System.Int32 class.

As a result “MyFavouriteNumber” becomes a member of the System.Int32 class. This class has a number of methods. There a basically two types of methods, Static methods, and Non-static methods. Static methods are the ones


c# – An intro to Arrays and Generics

Here are some terminologies:

Arrays: This is a object variable, that is designed to store a collection of object-variable. Each object variable is stored in numbered containers (i.e. they are indexed), with the first container starting at number 0.

Generics: these are like arrays, but they can only store builtin c# variables, i.e. variables that are int, string, double,…etc. However you can’t add new items to an array.

There are different types of generic collections:

  • List – these are like arrays, but you can also add new items to the list. Hence you can grow the list by using the “add” keyword
  • Queues – This is based on the idea that the first item in the queue gets processed first….i.e. it is based on first-in, first-out concept.
  • Stack – This is like queues, except it