Differences Between C++ And C#

Variables

Numeric Types

  • C# has sbyte which is signed 8-bit int.
  • C# has byte which is unsigned 8-bit int.
  • C# has uint which is unsigned 32-bit int.
  • C# has ushort which is unsigned 16-bit int.

Read-Only Variables

  • Member fields whose value can only be read.
  • readonly allows us to initialize an object at runtime but enforce read-only access.

Example:

public class Circle
{
    public double radius;
 
    public Circle(double rad)
    {
        radius = rad;
        PI = 3.14159;
    }
 
    public readonly double PI;
}

The Nullity of a Variable

  • When you declare a variable, it gets filled with garbage.
  • A value is referred to as null when it cannot be clearly determined.
  • In C#, normally, not all variables can hold null values. When declaring a variable, to indicate that it can hold either an actual value or it can be null, after its data type, add the question mark.

Example:

public class House
{
    string propertyNumber;
    char? propertyType;
    decimal? value;
}

Internal Keyword

  • The private keyword is used to let only members of a class access the (private) member of the class.
  • If you want to create a member of a class so that only objects of the same program can access that member, you can mark it with the internal keyword.

Expressions / Statements

foreach

Example:

foreach (int val in iarray)
  Console.WriteLine(val);

False Value

In C#, 0 is not interpreted as false.
So instead of writing if(!args.Length you should write if(args.Length == 0)

Types

The delegate Type

A delegate in C# is similar to a function pointer in C or C++. Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object.

Methods

Passing an Argument by Reference

  • To pass an argument as a reference, when defining the method, you use the keyword ref.

Example:

class Example
{
    void withOutReference( decimal amount)
    {
        amount = 777;
        System.Console.WriteLine("In getDeposit Amount = " + amount);
 
    }
    void withReference(ref decimal amount)
    {
        amount = 777;
        System.Console.WriteLine("In getDeposit Amount = " + amount);
 
    }
    static void Main()
    {
        decimal amt = 0;
 
        var exo = new Example();
        System.Console.WriteLine("Without Reference");
        System.Console.WriteLine("Amount before deposit = " + amt);
        exo.withOutReference(amt);
 
        System.Console.WriteLine("Amount after deposit = " + amt);
 
        System.Console.WriteLine("With Reference");
        System.Console.WriteLine("Amount before deposit = " + amt);
        exo.withReference(ref amt);
 
        System.Console.WriteLine("Amount after deposit = " + amt);
 
        System.Console.ReadKey();
 
    }
}

Passing an Argument Out

If you pass an argument out you must initialize it.

Example:

class Example
{
    void passArgumentOut(out decimal amount)
    {
        amount = 222m;
    }
    static void Main()
    {
        decimal amt = 0;
 
        var exo = new Example();
 
        System.Console.WriteLine("Pass Out");
        System.Console.WriteLine("Amount before deposit = " + amt);
        exo.passArgumentOut(out amt);
 
        System.Console.WriteLine("Amount after deposit = " + amt);
 
        System.Console.ReadKey();
 
    }
}

Classes and Objects

New objects

  • In C# when you declare an object using a class, you must allocate memory using operated new.
class House
{
}
 
class Program
{
    static void Main()
    {
        House property = new House();
    }
}

Nul objeccts

In C# you can create null objects. In C++ you can create null pointers.

class House
{
}
 
class Program
{
    static void Main()
    {
        House property = null;
    }
}

Class Nesting

  • A class can be created inside of another class.
  • The name of a nested class is not "visible" outside of the nesting class. To access a nested class outside of the nesting class, you must use the name of the nested class.

Example:

using System;
 
public class Outside
{
    public class Inside
    {
    public Inside()
    {
        Console.WriteLine(" -= Inside =-");
    }
    }
 
    public Outside()
    {
    Console.WriteLine(" =- Outside -=");
    }
}
 
public class Exercise
{
    static int Main()
    {
    Outside recto = new Outside();
    Outside.Inside ins = new Outside.Inside();
 
    return 0;
    }
}

Memory Management

Garbage Collector

  • C# has a garbage collector. We do not explicitly delete objects allocated through the new expression. In C++ it is the responsibility of the programmer to delete unused objects.

Pointers

  • C++ uses pointers. C# handles memory management and pointers are consider to be unsafe. To use a pointer you have to use the keyword unsafe.

Example: To compile the following program, you must indicate that you are using unsafe code. To do that, use the /unsafe modifier.

class Exercise
{
    unsafe static void Main()
    {
        int Length = 224;
        int *Len = &Length;
 
        System.Console.Write("Length ");
        System.Console.WriteLine(Length);
        System.Console.Write("Length ");
        System.Console.WriteLine(*Len);
        System.Console.WriteLine();
 
        Length = 804;
        System.Console.Write("Length ");
        System.Console.WriteLine(Length);
        System.Console.Write("Length ");
        System.Console.WriteLine(*Len);
    }
}

Anonymous Type

  • You don't have to use a class to initialize an object.

Example:

public class Exercise
{
    static void Main()
    {
        var BookInformation = new
        {
            Title = "Calculus 6e Edition",
            Pages = 1074
        };
 
        System.Console.Write("Title:         ");
        System.Console.WriteLine(BookInformation.Title);
        System.Console.Write("Number of Pages:  ");
        System.Console.WriteLine(BookInformation.Pages);
    }
}

Visual Studio Enhancements

Regions

You can create your own regions with with #region name and end it with #endregion.
Example:

class Circle
{
}
 
#region This section is reserved for quadrilateral shapes
class Rectangle
{
}
 
class Square
{
}
#endregion This is the end of the quadrilateral section
 
class Line
{
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.