Named argument, static class, parameter less constructor in c#

4
Named Arguments Visual C# 2010 introduces named and optional arguments. Named argument able you to specify an argument for a particular parameter by associating the argument with the parameter's name rather than with the parameter's position in the parameter list.Named arguments free you from the need to remember or to look up the order of parameters in the parameter lists of called methods. Example: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace StructuresConcept { class Program { //name paramete passing to a function with out mapping public static void UnMapFun(int id, int roll, string name, string adress) { Console.WriteLine(@"yourid is " + id + " your roll is " + roll + " your name is " + name + " your address " + adress); } static void Main(string[] args) { Console.WriteLine("this main"); UnMapFun(roll: 22, name: "adil", id: 2, adress: "koht"); Console.ReadKey(); } } } Why there is no parameter less constructor in structure? Although the CLR allows it, C# does not allow structs to have a default parameter less constructor. The reason is that, for a value type, compilers by default neither generate a default constructor, nor do they generate a call to the default constructor. So, even if you happened to define a default constructor, it will not be called and that will only confuse you. To avoid such problems, the C# compiler disallows definition of a default constructor by the user. And because it doesn't generate a default constructor, you can't initialize fields when defining them. Structs cannot contain explicit parameterless constructors. Struct members are automatically initialized to their default values. A default(parameterless)constructor for a structcould setdifferent values than the all-zeroed state which would be unexpected behavior. The.NetRuntime therefore prohabits default constructors for struct.

Transcript of Named argument, static class, parameter less constructor in c#

Page 1: Named argument, static class, parameter less constructor in c#

Named Arguments Visual C# 2010 introduces named and optional arguments. Named argument able you to specify an argument for a particular parameter by associating the argument with the parameter's name rather than with the parameter's position in the parameter list.Named arguments free you from the need to remember or to look up the order of parameters in the parameter lists of called methods. Example: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace StructuresConcept { class Program { //name paramete passing to a function with out mapping public static void UnMapFun(int id, int roll, string name, string adress) { Console.WriteLine(@"yourid is " + id + " your roll is " + roll + " your name is " + name + " your address " + adress); } static void Main(string[] args) { Console.WriteLine("this main"); UnMapFun(roll: 22, name: "adil", id: 2, adress: "koht"); Console.ReadKey(); } } }

Why there is no parameter less constructor in structure?

Although the CLR allows it, C# does not allow structs to have a default parameter less

constructor. The reason is that, for a value type, compilers by default neither generate a

default constructor, nor do they generate a call to the default constructor. So, even if you

happened to define a default constructor, it will not be called and that will only confuse you.

To avoid such problems, the C# compiler disallows definition of a default constructor by the

user. And because it doesn't generate a default constructor, you can't initialize fields when

defining them.

Structs cannot contain explicit parameterless constructors. Struct members are

automatically initialized to their default values.

A default(parameterless)constructor for a structcould setdifferent

values than the all-zeroed state which would be unexpected behavior. The.NetRuntime

therefore prohabits default constructors for struct.

Adil M
New Stamp
Page 2: Named argument, static class, parameter less constructor in c#

Or the big reason is that structure is value type and value type are initialized by default

value and constructor is used for initialization.

You don't have to instantiate your struct with the new keyword. It instead works like an int

you can directly access it.

Example of structure with out new key word using System; namespace StructureConcept { struct WithoutNew { public string name; public void printname() { Console.WriteLine(name); Console.ReadKey(); } } class Program { static void Main(string[] args) { WithoutNew s;//with out new key word s.name = "adil"; s.printname(); } } }

What is static class and how static modifier behave.

A static class is basically the same as a non-static class, but there is one difference

* A static class cannot be instantiated. In other words, you cannot use the new keyword to create a

variable of the class type. Because there is no instance variable, you access the members of a static class

by using the class name itself. For example, if you have a static class that is named Mcs3rd that has a

public method named Method Csharp you call the method as shown in the following example:

[public Static class Mcs3rd

{

Public static void Csharp()

{ }

}

To called there method we will use full qualified name like

Mcs3rd.Csharp();

Static class properties :

Page 3: Named argument, static class, parameter less constructor in c#

Contains only static members.

Cannot be instantiated mean we cannot make variable using new key word

Cannot contain Instance Constructors.

Only static constructor which will run once with making instance of class

A static constructor is used to initialize any static data, or to perform a particular action that needs

to be performed once only. It is called automatically before the first instance is created or any static

members are referenced.

using System; //no error but doing no thing and we not able to run namespace StaticClass { static class Program { int a; // you can declare but not good static void Main(string[] args) { //Program aaa = new Program(); erorr } public void aa() // not call this function by any cost { a = 33; Console.Write(a); } } }

Correct form of the following program

using System; namespace StaticClass { static class Program { static int staticVar; // make it static static void Main(string[] args) { staticVar = 333;//now accessable Program.StaticMethod(); }

public static void StaticMethod() // call this function by fully qaulified name now possible

{ staticVar = 33; Console.Write(staticVar); Console.ReadKey(); } } }

Page 4: Named argument, static class, parameter less constructor in c#

Garbage Collection

Garbage collector determine whether any object in the heap is dead or not being

used by the application. If such objects exist then memory used by these objects

can be reclaimed

In the common language runtime (CLR), the garbage collector serves as an

automatic memory manager. It provides the following benefits:

Enables you to develop your application without having to free memory.

Allocates objects on the managed heap efficiently.

Reclaims objects that are no longer being used, clears their memory, and keeps

the memory available for future allocations. Managed objects automatically get

clean content to start with, so their constructors do not have to initialize every

data field.

Provides memory safety by making sure that an object cannot use the content of

another object

Condition for garbage collection

The system has low physical memory.

The memory that is used by allocated objects on the managed heap surpasses an

acceptable threshold. This threshold is continuously adjusted as the process runs.

The GC. Collect method is called. In almost all cases, you do not have to call this

method, because the garbage collector runs continuously. This method is

primarily used for unique situations and testing.