1. Home
  2. Docs
  3. NCC
  4. Language Preliminaries
  5. Basic Language Constructs

Basic Language Constructs

Class and Objects

Often we know what is classes and objects are but when it comes to explaining, our mind goes blank. We are taught in academics that class is blueprint and object are the instance of class.

Well its true but lets see in the different way, a class is a design where we put the specs of what to be done and what to be included. And a object is just the unit of class or instance of class. Lets say a human class have common functionality, like eat, sleep, walk, run etc. And we all are the object of human class.


Namespace


Constructor

A constructor is a special method/functions that runs only once in its lifetime. It gets automatically invoked whenever the object (Instance of class) is created. As mentioned earlier, it is a special function, therefore it has the set of instructions about how the object is created. Usually, it is done to assign the initial values to the members or call a function whenever the class is instantiated. There are certain rules for writing a constructor.

  1. A constructor does not have any return type
  2. A constructor must have the same name as that of the class
  3. A class can have multiple constructor but with different parameters (Constructor Overloading)

public class SomeClass
{
   
    //some other functions / variables / properties 
}




=


public class SomeClass
{
    public SomeClass()
    {

    }
//some other functions / variables / properties 
}

Here, you can see that if there is no constructor defined then the class a a default constructor with no parameters in it and with no procedures. As we can see on the other side the constructor is blank and has nothing in its parameters and no steps defined, thus being equal.

public class SomeClass
{
    private int i = 0;
    public SomeClass()
    {

    }
    public SomeClass(int i)
    {
        this.i = i;
    }
}
class program 
{
    static void Main(string[] args)
    {
        // the object is created using the first constructor
        SomeClass Obj1 = new SomeClass(); 

        .......
        // the object is created using the second constructor
        SomeClass Obj1 = new SomeClass(10); 
    }
}

A small thing to remember

We can create the constructor as many as we want ensuring it doesn’t collide with the signature. The name of the parameter can be different but it will remain same as long as type is same. Example : SomeClass(int a) is same as that of SomeClass(int b).


Properties

Properties is a member of a class which provides the flexible mechanism for the classes to expose the private fields. Internally, properties are special methods called accessor. Usually, it has 2 accessors: a get property accessor and set property accessor. A get accessor returns the value and set accessor assigns a new value. the “value” keyword represents the value of a property. Property is nothing but a natural extensionof a data fields. Usually, we declare a private data field and a set of public get and set methods to access the data fields.

//A general property of type int
public int SomeVariable { get; set; }
//a general property of only get. A value can be read from here but we cannot write value to this propery
public int SomeVariable { get; }
// a general property having get and private set, that being said, 
//a variable can be read from anywhere but can be written privately inside the class.
public int SomeVariable { get; private set; }
// a full property that gets the value from some private variable 
//and set the value to the _someVariable

private int _someVariable;

public int SomeVariable 
{
    get 
    {
        return _someVariable;
    }
    set
    {
        _someVariable = value;
    }
}

Above code can be used as

// here the set accessor is called and "value" has 10 in it and 
//finally set accessor is called like 
SomeVariable = 10; 

.....
    set
    {
        _someVariable = 10;
    }

.....

// here the get accessor is called and 
//whatever the value is stored in _someVariable is returned  as 
int val = SomeVariable; 

.....
    get 
    {
        return _someVariable;
    }
.....

Arrays


Indexers


Structs


Enums


Partial Class

Partial class is not that hard to understand. It is just simple. It is a portion of a class within a same namespace, which combines as one during the compilation. We can write it in different files in different places but within a same namespace. A part of the class is written in one file and another part in another file. Usually we can see them while scaffolding or system generated classes.

Lets create a file FileA.cs

namespace SomeApp
{
    public partial class PartialClass
    {
        public void FunctionA()
        {

        }
    }
}

Now create a new file FileB.cs

namespace SomeApp
{
    public partial class PartialClass
    {
        public void FunctionB()
        {

        }
    }
}

Not implementing this partial class

PartialClass pc = new  PartialClass();
pc.FunctionA();
pc.FunctionB();

Being in the separate file,we can unify these files as it has the same name and same namespace, we can use it as if it is a single class.


Was this article helpful to you? Yes No

How can we help?

Leave a Reply

Your email address will not be published. Required fields are marked *