Fields, Modifiers allowed by Fields in C#.

Goud.Kv
Posted by in C# category on for Beginner level | Points: 250 | Views : 2519 red flag

C# is an Object-oriented programming language. C# comes with simplicity, expressiveness and great performance to meet the programmer productivity.
Recommendation
Read Class, Object and Class Inheritance in C# before this article.

Introduction

So far, we have seen class, object and class inhertances in C#. Now lets see fields and their modifiers in this chapter.

Objective

The main objective of this article is to understand about fields and their modifiers (allowed by fields) in C# programming.

Field

Field is a member (exvariable) of a class or struct. For example,
public class MyClass
{
    public int a;  // Field
    public int b;  // Field
}
  • In the above example, we used fields as public (which is not recommended). Generally they are private.
  • Objects and values in a class are also known as fields.
  • Fields allow the particular class to encapsulate the data.
  • We cannot access to the fields in a class from external classes.
  • Methods, indexers or properties are used to access them indirectly.
Declaring multiple Fields:
We can also declare multiple fields (in comma-separated list) to the same modifier like,
public readonly int fruits = 5, flowers = 8; 
The above code clearly explains that both the Fields (fruits and flowers) are sharing the same modifier (public readonly).

Modifiers allowed by Fields:

  • Fields allow different types of Access modifiers in C# such as public, private, protected and internal.
  • They allow 'new' which is an Inheritance modifier.
  • Fields allow volatile (Threading modifier) and unsafe (Unsafe code modifier).
  • More over they allow static (Static modifier).
  • They also allow readonly modifier.

Access modifiers:
Keywords used to provide the accessibility levels to a type (ex: variable, method) are known as access modifierspublic, private, protected and internal are the access modifiers used in C#.
              
  • public: Accessibility without any restrictions. A  type can be accessed by any other methods or classes in the same or different assemblies (which references it).
    Syntax is,
    public class MyClass
    {
        .........;        // It can be accessed by any one
    }
  • protected: It provides the limited access to the particular class and any other derived classes from it. A type can be accessed only by the methods or variables in the same or derived classes (class that inherits a protected class).
    Syntax is,
    protected class MyClass
    {
        .........;        // It can be accessed within this class and classes derived from it.
    }
  • internal: It provides access limited to the classes of current project assembly. A type can be accessed by any members in the same assembly and limits the accessibility for another assembly types.
    Syntax is,
    internal class MyClass
    {
        ..........;      // It can be accessed by classes of current assembly.
    }
  • protected internal: It provides both protected and internal access to the class i.e, limited to the classes of current project assembly as well as to the derived class types.
    Syntax is,
    protected internal MyClass
    {
        .........;       // It can be accessed by current project classes and derived classes.
    }
  • private: It is the default access modifier which limits the access within the class definition. This means, a type can be accessed only by the members of that class.  
    Syntax is,
    private class MyClass
    {
        .........;       // It can be accessed with in the class and within the assembly.
    }

static:
Static is a keyword (modifier) used with methods, classes, fields, constructors, operators, properties and events. It cannot be used with indexers and destructors.
  • static is used to declare a member (ex: variable). It denotes the singular types.
  • static improves the performance of the program but it provides less flexibility.
  • static member takes only one location in the memory and it will be shared from there.
  • static methods are faster than instance methods as they doesn't have instances, instead they are called with the type name.
Example:
static class MyClass
{
    static int a;               // static variable
    public static void fruits() // static method
    {
        .......;
    }
}
The above example explains the declaration of variables and methods using static modifier.

unsafe:
For some pointers which involves operations, requires an unsafe context. This type of context is denoted by unsafe keyword of unsafe code modifier.
For example,
class MyClass
{
    public unsafe void Test() // using unsafe keyword
    {
        int a;                // unsafe context
        int b;                // unsafe context
        // unsafe context.
        // pointers can be used here.
    }
}
The above sample example shows you the declaration of unsafe keyword to the fields.

volatile:
It is a keyword (modifier) which indicates that modification is done by the executing threads. This is generally used for the fields accessed by multiple threads without using any lock statements. This can be applied only for class or struct.
Example:
class MyClass
{
    public volatile int a; // public field as volatile

    MyClass(int b)
    {
        a = b;
    }
}
Volatile can be applied to the following types of fields,
  • Pointer types.
  • Reference types.
  • Enum types.
  • Integral types.
  • Generic types.

readonly:
It is a keyword (modifier) that is used to prevent a field from being modified after construction. Example,
public class MyClass
{
    readonly int fruits = 5; // Read only
}
In the above example, the field (fruits) has used readonly modifier in which the value of the fruits cannot be changed in the MyClass method.

Conclusion

In this article, we tried to understand fields and different types of modifiers that are allowed by fields. Hope you understand.

Thanks for reading.

Regards,
Krishna.

Recommendation
Read abstract, sealed and override modifiers in C# after this article.
Page copy protected against web site content infringement by Copyscape

About the Author

Goud.Kv
Full Name: Krishna Vamshi Goud
Member Level: Gold
Member Status: Member,MVP
Member Since: 2/12/2014 2:34:09 AM
Country: India
Thanks & Regards, Krishna


Login to vote for this post.

Comments or Responses

Posted by: Sheonarayan on: 8/27/2014 | Points: 25
In which scenario, unsafe keyword can be used? Please explain it.
Posted by: Goud.Kv on: 8/28/2014 | Points: 25
It is used for pointers and their operations as i already mentioned in it.

Thanks,

Login to post response

Comment using Facebook(Author doesn't get notification)