C# – Fields

Fields

Field is a member of a class or struct and it can be both instance fields or static. Instance specific is from the instance type, however it should only be used for private or protected accessibility.

class
{
  string name;
  public int Age = 10;
}

Fields have modifiers:

  1. Static: static
  2. Access modifier: public, internal, private, protected
  3. Inheritance: new
  4. Unsafe code: unsafe
  5. Read-only: readonly
  6. Threading: volatile

Field Initialization

Initializing field is optional and an uninitialized has a default value of either 0,\0,null,false. They get executed before the constructors.

public int Age = 10;

Declaring multiple fields together

Same type fields can be declared in comma separated list

static readonly int a = 6, b = 7;

Read-only Fields

Read-only fields gets initialized during run-time and can only be assigned during initialization.

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            A a = new A(10);  
            Console.Read();  

        } 
    }
    
    class A  
    {  
        public readonly int m;  
        public static readonly int n;  
        public A(int x)  
        {  
            m = x;  
            Console.WriteLine("" + m);  
        }  
        static A()  
        {  
            n = 100;  
            Console.WriteLine("" + n);  
        }  
    }
}

Datetime example

Can initialize readonly field in constructor but you cannot reassign later. Benefit – no one else can change the code from your team.

// you inline initialization also
readonly DateTime _startup = DateTime.Now;

C# – Class

Class

Is the most common reference type.

class MyClassName
{
   //methods, properties, fields, events, delegates, nested classes
}

Complex class has:

  1. Preceding the keyword class

    Attributes and modifiers such as Non-nested class modifiers

    1. public
    2. internal
    3. abstract
    4. sealed
    5. static
    6. unsafe
    7. partial
    8. private
  2. Following MyClassName
    1. Generic type parameters
    2. base class
    3. interfaces
  3. Within the braces

    Class members

    1. methods
    2. properties
    3. indexers
    4. events
    5. fields
    6. constructors
    7. overloaded operators
    8. nested types
    9. finalizer

C# – Operator Precedence and Associativity

Operator Precedence and Associativity

When expression contains multiple operators then precedence and associativity decide the evaluation order.

BODMAS is the order of precedence in general:

  • B – Brackets first
  • O – Of (orders: powers and square roots, cube roots, etc)
  • D – Division (left to right)
  • M – Multiplication (left to right)
  • A – Addition (left to right)
  • S – Subtraction (left to right)

Left-associative operators

Binary operators (except for assignment, lambda, and null-coalescing operators) are
left-associative; in other words, they are evaluated from left to right.

8/4/2 = (8/4)/2

Right-associative operators

The assignment operators, lambda, null-coalescing, and conditional operator are
right-associative; in other words, they are evaluated from right to left.

x = y = 3

It first assigns 3 to y and then to x

Null Operators

  1. Null-Coalescing Operator (??)
    If the left-hand expression is non-null right-hand expression is never evaluated.
  2. Null-conditional operator (?.)
    Allows to call methods and access members

C# – Using Namespaces

Namespaces

Namespaces are domain type names which makes it easier to find files and also it avoid name conflicts. They are independent of assemblies, which are units of deployment such as an .exe or .dll file.

The namespace keyword defines a namespace for types within that block
Example:

namespace Outer.Middle.Inner
{
class Class1 {}
class Class2 {}
}

the dots in the namespace indicate a hierarchy of nested namespace.

using Directive

The using directive imports a namespace.

using static (C#6)

Can import specific type with using static directive and all static members of that type can be used.

using static System.Console;

class Test
{
   static void Main() 
   {
      WriteLine ("Hello");
   }
}

The using static directive imports all accessible static members of the type, including fields, properties and nested types

You can also apply this directive to enum types using static System.Windows.Visibility;

var textBox = new TextBox { Visibility = Hidden ;}

Rules Within a Namespace

  1. Name scoping
    Names declared in outer namespace can be used unqualified within inner namespace. If you want to refer to a type in a different branch of your namespace hierarchy, you can use a partially qualified name.
  2. Name hiding
    If the same type name appears in both an inner and an outer namespace, the inner name wins.
  3. Repeated namespaces
    You can repeat a namespace declaration, as long as the type names within the namespaces don’t conflict
  4. Nested using directive
    You can nest a using directive within a namespace. This allows you to scope the using directive within a namespace declaration.
  5. Aliasing Types and Namespaces
    You can import just the specific types you need, giving each type an alias

Advanced Namespace Features

  1. Extern
    Extern aliases allow your program to reference two types with the same fully qualified name
  2. Namespace alias qualifiers
    Names in inner namespaces hide names in outer namespaces.